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 n00dle · May 11, 2014 at 03:01 PM · listremove

Removing object from a list also removes it from a different list? help!

Right, my brain has melted and google has failed me. The following script is just a test, on playing in populates list1 with all transforms inside a trigger tagged "tagged". Pressing 'K' then fills the list2 with all transforms in list1 Pressing 'O' chooses the first transform in list2 pressing 'L' removes the transform from list2

Now, this works almost as required ... but it also removes the transform from list1 even though i specifically asked it to only removed from list2. Im obviously missing something here but after hours and hours i can't figure it out, please help!

 public Transform transform1;

 public List<Transform> list1;

 public List<Transform> list2;


 void Start () 
 {
     list1 = new List<Transform> ();
     list2 = new List<Transform>();
 }
 
 void OnTriggerEnter (Collider other)
 {
     if (other.tag == "tagged")
     {
         list1.Add(other.transform.root);
     }
 }
 
 void OnTriggerExit (Collider other)
 {
     if (other.tag == "tagged")
     {
         list1.Remove(other.transform.root);
     }
 }

 void Update()
 {
     if (Input.GetKeyDown ("k"))
                     list2 = list1;

     if(Input.GetKeyDown("o"))
                     transform1 = list2 [0];

     if (Input.GetKeyDown ("l"))
     {
         list2.Remove(transform1);
     }
 }

}

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
2
Best Answer

Answer by robertbu · May 11, 2014 at 03:11 PM

A list is a reference or a pointer to a pool of memory. When you do this:

 list2 = list1;

...you create two variables pointing to the same pool of memory. If you want to copy the array, you have to do something like:

 list2 = new List<Transform>(list1);

Now 'list2' now points to its own pool of memory and has a copy of all the entries in 'list1'. But there is still something to pay attention to here. Say you did this:

 list2[2].position.x = 3.5f;

If the transform pointed to by 'list2[2]' exists in both lists, then the 'position.x' changes in both lists. That is because the Transforms that you put into the two lists are also by reference, and entries in the list that point to the same Transform will both have their data changed.

For more information Google ".NET shallow deep copy." Here is one hit:

http://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy

Comment
Add comment · Show 1 · 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 n00dle · May 11, 2014 at 03:44 PM 0
Share

Thanks guys!

avatar image
1

Answer by Deign · May 11, 2014 at 03:09 PM

It is because you're setting list2 = list1. This does not make a copy of the list, it simply makes a reference to the first list. Try this instead:

 list2 = new List<Transform>(list1);
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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

How to remove project from the Project Wizard 6 Answers

List That Won't Remove The Last Two Elements 3 Answers

identify and remove an item from a list 1 Answer

Problems with removing a random from a list 2 Answers


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