The docs say this about destroying objects in an array using DestroyImmediate:
“Also note that you should never iterate through arrays and destroy the elements you are iterating over. This will cause serious problems…”
Surely they mean a dynamic array? What’s the problem with doing it on a static array? I understand on a dynamically allocated array that the contents may be shuffled/removed, but I thought it was safe on a static array.
I’m writing an editor script (C#) for which I need to Instantiate clones of a GameObject and store them in this static array:
private GameObject[] cloneSteps = new GameObject[12];
Further on I need to destroy them before re-instantiating them, so I do iterate over them:
if(cloneSteps != null){
for(int i=0; i<12; i++)
{
if( cloneSteps *!= null)*
_ DestroyImmediate(cloneSteps*);_
_ }_
_}*_
I have been getting an intermittent bug, so I’m wondering if this is faulty?
How else can you destroy/remove/delete objects in an array?
Am I right in thinking that there will be a memory leak if I simply overwrite the array before destroying the elements?
Thanks
Steve