function being called twice at the same time causing an error.

I have an object that detects (via onTriggerEnter) when units enters an area and it pushes them onto an array. when a unit dies he sends his target numer (which is his position in the array) to the array, so it can null him out, and everything to the right of him shifts down to close the gap, then the array resizes -1 to pop off the last space which is now null. it all woks as intended.
But when i start to have massive numbers of enemies battleing at once. every once in a while two things will die close enough to where it seems they get batched into the same update call i guess? so the previously mentioned function, that extracts a badguy, then ends up running twice at the exact same time, causing an error because two functions are trying to manipulate one array, and it just goes all wrong.
Is there some way to detect if the function is being called more than once at a single instance? or am i just gonna have to revise my code to work around this?
here is the function in question as of right now:

public void ExtractBadGuy(int tNum)
		if (badGuys.Length == 0)
			Debug.Log("Extracting a badguy from empty list. wtf.");
		else if(badGuys.Length > 1)
			for(int i = tNum; i < badGuys.Length-1; i++)
				badGuys *= badGuys[i + 1];*
  •  	}*
  •  }*
  •  System.Array.Resize<Transform>(ref badGuys, badGuys.Length - 1);*
  •  ReAssignBadGuyNumbers();*
  • }*

if badGuys is static, and there are multiple threads running this code, you’d have to either invent another storage strategy, or consider using a lock. There are several typical strategies, but none involve inquiring about whether a function is being called by more than one thread, though there can be counts and or semaphores that do that, there isn’t a general method of inquiring about such a scenario.

A treatise on the subject is best found in the documentation on C# threading (Google provides, search for C# thread synchronization, C# mutex, C# lock statement, and consider AutoResetEvent as part of the study).

The basic objective is to surround all operations that are performed on a “public” resource such as badGuys appears to represent, so that a lock of some form is obtained, the work is performed, then the lock is released. When two threads collide on the same on the resource, only one will be allowed to proceed at at time, each in turn (though in no particular order). Unfortunately it appears that you’ll have several locks to apply, not just on the section posted, but on anything that accesses badGuys, because this manipulation could corrupt any process that is merely reading an entry in the badGuys array.

That said, there’s a lot to be said for not scooting the array. It takes time and may not be all that necessary. It is possible you could merely mark entries that should be considered as removed, such that the rest of the code can recognize and thus skip them, with the additional ability to re-use such marked entries instead of creating new ones. That said, you may still have a synchronization problem, for several reasons, but none the least for finding an available “marked” entry for re-use which may be performed by multiple threads.

To that end, there may be no good reason to resize the array as you’re doing. Let it waste a little space along the way to save time and avoid generating garbage for the memory manager to clean up.

Depending on your requirements, you may find that a Dictionary or HashTable might perform better than scooting arrays around like this, but that requires a bit of analysis I can’t perform from just a snippet of code.