Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by WesternRider · Jul 08, 2012 at 10:12 PM · listsadd

Problem with "Add" method for List

Hi

I have made the following test script to highlight a problem I have with adding elements to a list - it is a list of object descriptions, where object descriptions is defined as a class. My test script looks like this:

 #pragma strict
 
 class MyObjectDescription {
    var objectName : String;
    var isCombinedMesh : boolean;
    var objectInPlace : boolean;
 }
 static var myObjectList = new List.<MyObjectDescription>();
 static var myCurrentObject = new MyObjectDescription();
 
 function Start () {
 
     // Assign the name Joe to myCurrentObject
     myCurrentObject.objectName = "Joe";
 
     // Put this myCurrentObject in myObjectList - the first item added
     myObjectList.Add(myCurrentObject);
     print ("List index 0: " + myObjectList[0].objectName);
 
 
     // Assign the name Bob to myCurrentObject
     myCurrentObject.objectName = "Bob";
 
     // Put this myCurrentObject in myObjectList - the second item added
     myObjectList.Add(myCurrentObject);
     print ("List index 0: " + myObjectList[0].objectName);
     print ("List index 1: " + myObjectList[1].objectName);
 
 
     // Assign the name Laura to myCurrentObject
     myCurrentObject.objectName = "Laura";
 
     // Put this myCurrentObject in myObjectList - the third item added
     myObjectList.Add(myCurrentObject);
     print ("List index 0: " + myObjectList[0].objectName);
     print ("List index 1: " + myObjectList[1].objectName);
     print ("List index 2: " + myObjectList[2].objectName);
 }

When I run this script the Add method doesn't just add a new member to the list, it assigns the same content to all existing members in the list as well.

What am I doing wrong? I wanted to have the list become "Joe", "Bob", "Laura".

But the print out looks like this:

alt text

printout.png (15.7 kB)
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by hathol · Jul 08, 2012 at 10:33 PM

Your problem is not the list but the "myCurrentObject" variable. Classes are passed by reference, that means instead of copying the content of a memory location, only the address of that location is copied into the list. So when you do something like

 myObjectList.Add(myCurrentObject);

both, myCurrentObject AND the object in the list will both point to the exact same object in memory. When you now change myCurrentObject.objectName, that name will change for myCurrentObject and the object in the list.

To prevent that behaviour, you can create a new object (and therefore reserve a new memory location). So you code should look more like

 myCurrentObject.objectName = "Joe";

 // Add a copy of the memory address that myCurrentObject points to to the list
 myObjectList.Add(myCurrentObject);

 // create a new object in memory
 myCurrentObject = new MyObjectDescription();
 // Assign the name Bob to myCurrentObject
 myCurrentObject.objectName = "Bob";

 // Add a copy of the new memory address that myCurrentObject points to to the list
 myObjectList.Add(myCurrentObject);
Comment
Add comment · 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 WesternRider · Jul 08, 2012 at 11:26 PM 0
Share

Super! I get it! Adjusted my test script and it works perfectly :-) Thanks, $$anonymous$$!

avatar image Casper_Stougaard · Nov 06, 2013 at 01:45 PM 0
Share

This answer has also helped me a lot :) Great answer, thank you!

avatar image
1

Answer by UndergroundDevelopment · Aug 24, 2020 at 10:57 AM

this is Wayyyyyyyyyyyyyyyyy old but for those who are new here seeing this. You could also do

myObjectList.add(myCurrentObject = new MyObjectDescription);

something shorter than the answer above.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem adding to a list (count always returning 0) 0 Answers

List add overwriting first element even through I'm instantiating a new one? 3 Answers

Check if Exists and if so, Add it to Other List 1 Answer

Help: items not getting added to list. 1 Answer

can add to a list but not remove 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