- Home /
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.
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
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.
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.
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.