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 Maker1000 · Aug 17, 2013 at 09:02 PM · list

List elements all changing

I have a list of type Foo. I create one Foo object and add it to the list. Foo has a Copy() method whereby it is supposed to create a new Foo object and assign all of its values to the new object and then return the new Foo object. That new Foo is then added to the list. After the new Foo is added, some of its member values are randomly changed, and here is where it gets crazy. When a value on the most recently copied Foo is randomly changed, the values of that member are changed on all of the other copies of the Foo. Does anyone have a clue why?

Comment
Add comment · Show 4
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 save · Aug 17, 2013 at 09:15 PM 0
Share

I think I might understand the problem, the copy you're doing is a shallow copy which shares the same memory address as all other Foo's. What does the Copy() method look like?

avatar image Maker1000 · Aug 17, 2013 at 09:21 PM 0
Share

function Copy() : DiscreteTrait { var newTrait = new DiscreteTrait(trait,a1,a2); newTrait.$$anonymous$$utate(); return newTrait; }

a1 and a2 are the Foo objects I was mentioning. 'trait' wasn't being affected.

avatar image Maker1000 · Aug 17, 2013 at 09:24 PM 0
Share

If I understand your response correctly, a1 and a2 are memory addreses of the object, and so any changes are being applied at the addresses of the original object?

avatar image save · Aug 18, 2013 at 10:42 AM 0
Share

Sorry for the delayed answer, it was late over here. :-) I wrote an answer below, I'm no computer scientist but from what I understand your issue is that your objects are addressed to the same object in memory and makes them become dependent.

1 Reply

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

Answer by save · Aug 18, 2013 at 10:35 AM

If changes are being applied to all of the objects when you change one, the reason is that they are addressed to the same origin and are just 'mirrors' of that same object cached in memory.

What you can do is to iterate through a Foo and return a fresh instance of it, it's a bit of a workaround but a shortcut considered what kind of reflection you might have to use instead if your 'bars' isn't that many. Here's an example of what you can do:

 private var fooList : Foo[];

 function Start () {
     // Create the Foos
     fooList = new Foo[2];
     for (var f in fooList)
         f = new Foo();
     
     // Set a value for Foo[0].bar
     fooList[0].bar = 1337;
     
     // Copy Foo[0] into Foo 1
     fooList[1] = Foo.Copy(fooList[0]);
     
     // Set Foo[0].bar to 0
     fooList[0].bar = 0;
     
     // Log what happened, Foo[0] should be 0 and Foo[1] should be 1337 if they are independent
     for (var x = 0; x<fooList.Length; x++)
         Debug.Log(fooList[x].bar);
 }
 
 class Foo {
     var bar : int;

     // This is what you would do to create an independent instance:
     static function Copy (fooCopy : Foo) : Foo {
         var returnFoo : Foo = new Foo();
         returnFoo.bar = fooCopy.bar;
         return returnFoo;
     }
 }
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

15 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

Variable Scroll How do I make it Scroll 1 Answer

Question About Clone 0 Answers

sort List alphabetically 2 Answers

Creating a list with trigger 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