Emptying a Generic List

Hey,

I’m not very familiar with the various lists that are available, but I’ve tried to implement something using generic lists. Essentially each game tile has a generic list that keeps track of all game objects with a particular tag that are present (the order isn’t important, I only need to know what is there).

Given that it is a turn based game, and that I don’t need to know the order of the list, and also that the list is changed when an object moves between game tiles - I figured that it would be ok to just clear the list and use Physics.OverlapSphere to update the objects present whenever anything changed.

This works fine for all situations I expect so far, apart from one, which is where two objects merge. In doing that, I delete one of the game objects and then use Clear() on the array, and call the Physics.OverlapSphere, but this doesn’t seem to work. It appears that the reference to the game object that was deleted causes a problem - the editor shows “Missing (Game object)” in the slot in the list that it used to occupy. This also seems to happen regardless of whether I try to clear the list before deleting the game object.

Am I misunderstanding something here, or is that how it should behave?

Thanks,

Matt

When you delete the game object, remove it from the list(s) that it is in.

Thanks, I had been trying to do that with the Clear() function. After a lot of playing around I noticed that the OverlapSphere part of the code wasn’t quite doing what I had expected (the info being sent to it was not what I had intended). After cleaning that up the code works fine now.

Cheers,

Matt