I have a list of game objects, this keeps track of the monsters i have in my game. when the monster is killed, it will be in position 0 of the list. i want to destroy the object in the scene and remove it from the list.
My question is, does the command: list.RemoveAt(0); call the object to get destroyed or does it leave it existing as some sort of memory leak?
I have tried removing the monster, destroying then removing, saving the monster to a var then removing then destroying and all of them cause bugs. If i knew how the command works, i could narrow down the issue to that or something else in my code.
CodePudding user response:
If you make the list a public variable you can see it in the Unity inspector. When you destroy a monster you will see the list change to have the monster set to null. I would remove the monster from the list before destroying. You can call the OnDestroy method on the monster to remove the monster from the list before it gets destroyed. This uses the list's Remove function instead of RemoveAt.
CodePudding user response:
No, removing it from the List won't destroy it. List.Remove() does not destroy GameObjects - it is just your personal list. You would need to call GameObject.Destroy(list[index]); and then you could list.RemoveAt(index); to remove it from your list.