|
Post by britannis on Apr 5, 2016 2:09:03 GMT
I have a suspicion the reason why my foreach() loop isn't destroying all my objects at editor time when I click on my editor button is because it takes longer than one frame to destroy all my objects.
foreach (Transform child in obstaclesParent) { DestroyImmediate(child.gameObject); } The above loop is called when I click on an editor button, but I notice I have to click the button multiple times before all my objects are destroyed. I tried creating a coroutine but I got weird results.
How should I delete a large number of objects at editor time?
|
|
|
Post by Admin on Apr 5, 2016 15:05:58 GMT
The "foreach" loop has some special restraints on it that a normal "for" loop doesn't. For example, if you are modifying a collection while iterating over it then it will break. I don't know that it will fix your problem, but I would have tried something like this:
for (int i = obstaclesParent.childcount - 1; i >= 0; --i) { DestroyImmediate(obstaclesParent.GetChild(i).gameObject); }
*Note that I didn't try compiling this code and am typing it from memory so its entirely possible that I got something a little off - hopefully it just gives you the correct general idea.
Also, I think "DestroyImmediate" is synchronous and that it doesn't wait a frame to act on the game object like "Destroy" does. It should be able to happen in one frame regardless of how long it takes.
|
|