• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Joneson Oh · Dec 24, 2009 at 05:36 AM · customizationjavascript-specificsubclassing

Creating an array of custom objects

Hi all,

I have created a custom class of my own in javascript with attributes as shown below:

class ConceptNode{ //node to store concept/relation

static var concept = ""; static var activity = -1; static var activated = false; static var hyperConceptOf = new Array(); static var elementConcept = new Array();

function ConceptNode(con,act,hyper,ele){//constructor this.concept = con; this.activity = act;
hyperConceptOf.push(hyper); elementConcept.push(ele);
}

}

I then try to create an array of objects of the above class in another script which is attached to a GameObject using the following:

static var conceptList = new Array(); static var conceptNodeList = new Array();

function Update(){

}

function Start(){
conceptNodeList[0] = new ConceptNode("Landmark",0,"","Building");

conceptNodeList[1] = new ConceptNode("Building",0,"Landmark","Commercial");

for(var i=0;i<conceptNodeList.length;i++){
    Debug.Log(conceptNodeList[i].concept);
    Debug.Log(conceptNodeList[i].hyperConceptOf[0]);
}

}

It seems that both the ConceptNode objects which I've created in the conceptNodeList are the same object. The log messages show the same attributes for both iterations. is this due to javascript array passing by reference? Or am I doing something wrongly? Any help by anyone would be greatly appreciated.

Thank you

Comment
CJCurrie
cregox

People who like this

2 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Stelimar · Dec 24, 2009 at 05:48 AM

Your variables shouldn't be static. static variables are the same for every instance of a class, which is why you're getting the same values no matter which instance you access. Just change this:

static var concept = "";
static var activity = -1;
static var activated = false;
static var hyperConceptOf = new Array();
static var elementConcept = new Array();

To this:

var concept = "";
var activity = -1;
var activated = false;
var hyperConceptOf = new Array();
var elementConcept = new Array();

Unless of course you actually need any of those variables to be static.

Comment
Bampf
cregox
x4000
dibonaj
CaptainKirby

People who like this

5 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joneson Oh · Dec 24, 2009 at 05:58 AM 0
Share

Thank you so much for your prompt and accurate answer Stelimar. That solved my problem. Cheers to you!

avatar image cregox · Feb 25, 2010 at 09:12 PM 0
Share

So sad when new users reciprocate by saying "that solved my question" and don't realize there's an "Accepted" button right there. :( By the way, this solved my question too! :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Subclassing no-go? (C#) 1 Answer

Character Customization tutorial, Blender considerations 0 Answers

Character Customization 1 Answer

Best pipeline to import multiple weapons into Unity for characters? 2 Answers

help with upgrade system(Getcomponent) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges