Ehhm, you may want to re-read what you just wrote, go through your code step by step in your head and think about what you’re doing. Your current code removes the last object from the list. After that you grab the now last object and destroy it, while keeping the reference to that destroyed object in the list… You may want to re-order your code to
thank you for info.
This is what got me in List there apparently is separate index list and object list. I come from UE4 Bp’s and there you can remove index at or just item.
Huu? I’m not sure what you’re talking about. A generic list in C# does not have a seperate “index list”. It’s just an ordinary array wrapped in a class with a counter, nothing more (well a bit boilerplate but essentially that’s it). Here’s a reference implementation of the class.
Maybe you are confused about “removing an element from a list” and “destroying a gameobject”. Those are two completely seperate things to begin with. The List class also has a Remove method which can remove an element based on the element value. However that’s less efficient since that method has to first find the provided value inside the list and then remove the element at that index. So when you know the index using RemoveAt is way faster, especially when it’s the last element.
When you put a gameobject reference into a list, all you do is storing a reference to the gameobject in that list. The list is not responsible or in any way related to the lifetime of the gameobject. The gameobject lives in the scene. You can completely get rid of the List and the gameobjects would still be there unless you destroy them. Removing an element from a list will completely remove it from that list, just like when you remove the name from the list of people you want to invite to your birthday party, that doesn’t kill that person or make it vanish. The name simply is no longer in the list.