Emptying a Generic List / Unexpected Behaviour

I am working on a simple tile based game and I have created a class container (TileLocations) to hold a number of variables that record useful information about a number of locations within each game tile. The one that is causing me problems is a generic list that I use to record a list of the current units that are present in that part of the game tile:

class TileLocations extends System.Object{
	var unitsPresentList	= new List.<GameObject >();	
var tileLocation = new TileLocations[9];

This is probably not the most efficient way to do it, but it makes dealing with the logic of game behaviour much easier for me to understand (and it’s all visible in the inspector when I click on each game tile). The problem is that I can’t seem to get the array list to behave as I would expect. I am using the colliders to detect which units are present on the tile with:

	var colliders : Collider[] = Physics.OverlapSphere (this.transform.position, searchRange);	
	for (var hit : Collider in colliders) {
        	if (!hit){
		if (hit){

…and I am attempting to clear the generic list prior to filling it with the above code, by doing this:

function ClearArmiesInTileLocation(locationNumber: int){	

However, I seem to be ending up with some strange inconsistencies. Occasionally I will get “missing gameObject” in the array - I assumed this must have been when a unit is destroyed and is no longer references in the array, but I always clear the array prior to destroying units, so there should never be any units in the array at the time that the unit is destroyed. On occasion I also seem to be getting duplicates of the same objects in the arrays.

Am I misunderstanding how these things are handles in memory? Am I not actually removing everything from the arrays (hence things getting reported missing when I later destroy their gameObjects), or duplicates appearing?

Alternatively, are there any similar concepts that might work better? I’d rather not recode everything from the ground up, since this is actually efficient enough for my game (no noticeable slow down or hangs while rebuilding arrays) - I’d rather iron out my method, or refine the way in which objects are added and removed from arrays.


I managed to locate the issue, with another focussed debug effort. However, I don’t fully understand the reasoning for why the solution fixes my issue.

I was updating the unitsPresentArray first, then the armiesPresentArray (not fully mentioned above), but for some reason the update of the armiesPresentArray with Physics.OverlapSpehere was interfering with the first Physics.OverlapSphere. Printing a list of hit objects from the first showed that some hit in the second were also being counted as a hit in the first. I put “Yield;” between them and it seems to adequately separate the processes - however I don’t understand why it needs that to work.

Regarding the missing objects, I had assigned the collider used in this process to variable, but I had forgotten to update my code to reflect that (I was accessing the wrong collider) so the object was never actually removed from the array, and consequently would return a missing object error when it was deleted.