Destroy in editor mode


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?


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:

  1. Iterate through the list and keep the indices of the objects you want to destroy
  2. Then go through each of these indices and destroy those objects only.
  3. You might need to sort your array again as there will be some null objects in it now.

Good luck