- Home /
Destroy in editor mode
Object.DestroyImmediate
From Unity reference: 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).
Hm, what would be a proper way to do it in the editor then? Say I need to destroy half of its contents?
Thanks!
Could you clarify what is "its" in the "half of its contents"
Thank you, it's the contents of array, let's say, the game objects
Answer by ZenithCode · Apr 19, 2014 at 08:06 AM
The Unity recommendation is right. The problem is that you would be iterating through a list/array with a continuously changing length.
One way to do it is to:
Iterate through the list and keep the indices of the objects you want to destroy
Then go through each of these indices and destroy those objects only.
You might need to sort your array again as there will be some null objects in it now.
Good luck
Your answer
Follow this Question
Related Questions
Problem with Object collision 1 Answer
Animating an object so it doesn't bounce to the initial location the animation was recorded 1 Answer
Add custom properties to Objects 1 Answer
Object vanishes, but casting shadows, in game window but still visible in editor 0 Answers
Is it possible to display Unity 3d object on the video screen ? 0 Answers