I’m not sure what’s your actual problem. First of all you should be more precise about what you want to do (maybe include a code fragment) AND what kind of “Array” you use. Native arrays can’t be resized so you can’t add or remove elements. If you talk about a List container or the UnityScript Array-class it’s a different story.
Beside that i don’t see any need for a mutex since Unity’s scripting environment runs in a single thread so there are no concurrent scripts.
Unity’s Destroy function is delayed until the end of the current Update().
Basically you can remove elements from a List at any time, except when you currently iterate through the collection. If you use a for-each loop it will throw an exception when you change the collection inside the loop. The easiest way to prevent such errors is to use a “normal” for loop and go backwards.
// C#
List<GameObject> myList;
for (int i = myList.count-1; i >=0; i--)
{
// do something with myList
// It’s safe to remove it from the list, since the indices before this item will stay the same if (ShouldBeDeleted) { Destroy(myList*);* myList.RemoveAt(i); } } The only thing you have to keep in mind is if other scripts holds a reference to an object that has been destroyed, it’s reference will become “null”. References in any kind of collections aren’t removed automatically when they are null. If you think that an object can be destroyed from another script and you still hold a reference to it, make sure you check the reference against null before you use the reference: // C# public GameObject myObject;
I had been doing a null check, so I’d put the funny behaviour after the Destroy statements down to concurrency issues. It’s good to know it’s not as complicated as that