- Home /
Why is iterating an array and destroying objects considered bad practice?
I am writing a "match-3" style game. My matching algorithm builds an array of objects which need to be destroyed. That is, after my match check, I iterate through the array, destroying the objects in it.
While I realize I should use Destroy over DestroyImmediate, I'm really curious about this comment in the reference manual:
Also note that you should never iterate through arrays and destroy the elements you are iterating over. This will cause serious problems (as a general programming practice, not just in Unity).
(http://docs.unity3d.com/Documentation/ScriptReference/Object.DestroyImmediate.html)
Why is this bad practice - This is exactly what I'm doing and I'd like to know what "serious problems" they are referring to. I assume an array of objects is in fact an array of references to objects, so I'm not sure why this is bad practice to delete the referenced object and what harm it will cause.
And, in light of that, how would one delete the objects which you have collected in an array if you can't iterate them?
Thanks for any insight,
Chris
Answer by SoundLogic · Sep 02, 2013 at 06:09 PM
Suppose I have an array {5,4,4,3,4,6}, and I want to remove all the 4s
for(int i=0;i<array.length;i++)
{
if(array[i]==4)
array.remove(i)
}
now this code will go:
i=0:not a 4. Do nothing. Result: {5,4,4,3,4,6}
i=1:a 4. Remove. Result: {5,4,3,4,6}
i=2:not a 4. Do nothing. Result: {5,4,3,4,6}
i=3:a 4. Remove. Result: {5,4,3,6}
i=4:i>=array.length. End.
final result: {5,4,3,6}
Note that not all 4s have been removed. See the problem?
This doesn't explain why you can't iterate through it backwards...
Answer by Rawkjs · Sep 02, 2013 at 07:34 PM
This is indeed bad practice for anything that is not handled by Unity. If you were to loop in an array of GameObject or components, this would not really matter if you use the Destroy method. Here's why :
As suggested by SoundLogic above, when iterating over an array and removing objects from the array, you might end up skipping objects.
If you only use the Destroy or DestroyImmediate though, the object will be disposed of but will not be removed from your array. Which means that the iteration will got to completion. Beware though as this means that the disposed objects will stay in your array. This can be a problem if your array doesn't only serve the purpose of destroying objects.
The best way to actually remove/destroy objects from an array while iterating over it without consequences would be to do a reverse loop as such :
for (int i=array.Count; i >= 0; i--) {
if (array[i].NeedsDisposing) {
Destroy(array[i]);
array.remove(i);
}
}
This way, you can remove objects in a loop without getting an out of bounds exception!
So what is actually happening to the array of GameObjects in memory? Is it that Object.DestroyImmediate
is blowing away the reference in the array, which the Enumerator needs in order to function properly or something?
Your answer
Follow this Question
Related Questions
Iterating through and deleting gameObjects 1 Answer
"For"-Loop only works for final element in array 1 Answer
How can I parent each item from one array to each item in another array? 1 Answer
Instantiate same PreFab to an array of GameObject by RayCast. 1 Answer
Can't copy Gameobject into array, only refer to an existing one. 1 Answer