Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 SmugMonster · Dec 15, 2019 at 02:21 AM · listsadd

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

Hey folks. Quick question. I've got this for loop happening: //possibleMoveTiles is a list of GameObjects and has the expected count, let's say 3:

 for(var i = 0; i < possibleMoveTiles.Count; i++)
                 {
                     newList = new List<GameObject>();
                     var temp_Tile = new GameObject(); 
                     temp_Tile = possibleMoveTiles[i];
                     newList.Add(temp_Tile);
                 }
 //newList.Count returns 1 here where I would expect 3
 

I'm guessing temp_Tile ends up pointing at the same reference, but I'm not sure why.

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

3 Replies

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

Answer by Larry-Dietz · Dec 15, 2019 at 10:07 PM

I agree with Bunny83 entirely 100% on this.

However, that being said, I would guess you are trying to add every gameobject in possibleMoveTile to your list.

Here are a quick easy way you can do this.

      newList = new List<GameObject>();
      for(var i = 0; i < possibleMoveTiles.Count; i++)
         { 
             newList.Add(possibleMoveTiles[i]);
         }
 

Hope this helps, -Larry

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 Bunny83 · Dec 16, 2019 at 12:19 AM 0
Share

If he just wanted to copy one list into the other he could just do

 newList = new List<GameObject>(possible$$anonymous$$oveTiles);

or with Linq just

 newList = possible$$anonymous$$oveTiles.ToList();

without any loop required. However I think he probably want to create a gameobject / instantiate an object for each element (which he does but he does not keep the references to those objects). As I said it's completely unclear what's the point / goal of the code.

avatar image
0

Answer by Bunny83 · Dec 15, 2019 at 12:43 PM

Pretty much none of your lines make much sense.

This line:

 newList = new List<GameObject>();

creates a new empty List and replaces the reference in the newList variable with the new empty list. The old list will be up for garbage collection since no variable is referencing it anymore. You do this every loop iteration. So if you have 3 elements in your possibleMoveTiles list you will create a total of 3 seperate lists, though only the last created list will of course remain in the newList variable.


In this line:

 var temp_Tile = new GameObject(); 

you actually create a new empty gameobject in the scene and store the reference to that gameobject in the local temp_Tile variable. However in the next line you again replace the reference by the object reference stored in your possibleMoveTiles at index "i". GameObjects will stay in the scene even when you don't reference them anymore. So after your loop (assuming 3 elements) you will have created 3 empty gameobjects which you do not reference anywhere anymore.


finally this line:

 newList.Add(temp_Tile);

will add the reference stored in temo_Tile in the list that is currently referenced by your newList variable. Since you create a new list every iteration, that means each of your 3 lists will only receive one element. However as already mentioned above only the last list will survive.




I don't really understand what you actually try to do here. So I can't really suggest any fixes since we have absolutely no idea what you intentions are. It seems you have some basic misunderstandings of the code in general. You may want to look up some basic C# tutorial.

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

Answer by SmugMonster · Dec 15, 2019 at 10:55 PM

Thanks guys - I actually twigged that at about 1:30 in the morning, moved the list instantiation outside the for loop, and it worked.

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

119 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 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 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 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 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 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

Problem with "Add" method for List 2 Answers

How to add an item to a list with a class/uJS struct? 1 Answer

Adding object to list add the same object to another list 1 Answer

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

Help: items not getting added to list. 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