destroyImmediate on children not working correctly?

So far I have managed to get around this problem by doing other methods instead, but it seems than when running a foreach loop like so:

foreach(Transform child in star.transform){
					DestroyImmediate(child.gameObject);
				}

It will destroy some but not all, this time around it leaves 1 alive, last time i had this problem with something else it left 3. I THINK the ones left alive were gameobjects that had been created before runtime, then been through a cycle at runtime.

For example: i create the object, press play, press stop. Now if i run my method to destroy all child transforms that object is no longer destroyed. IE it seems to no longer be considered inside “star.transform”. What am i missing here?

Many thanks!

This answer doesn’t exactly answer your problem but it’s actually the same issue. You shouldn’t use DestroyImmediate anyways. It’s mainly for use inside the UnityEditor like you can read in the docs.

So the usual way is to simply use Destroy instead of DestroyImmediate. Destroy is only delayed until the end of the current frame.

edit
So from your comment it seems like you are actually writing an editor script. In this case you have to use a temporary array / list and iterate over this. The IEnumerator of Transform uses childCount and GetChild to iterate through the childs. So modifying the collection while you iterate through them won’t work. Use this instead:

var tempList = transform.Cast<Transform>().ToList();
foreach(var child in tempList)
{
    DestroyImmediate(child.gameObject);
}

Don’t forget to add the “System.Linq” namespace.

I ran into a similar problem using this worked for me

while(transform.childCount != 0){
			DestroyImmediate(transform.GetChild(0).gameObject);
		}

Piesk:
Seems to work for me when deparenting the obj first in Unity 5.5: `
oldTile.transform.SetParent(null);
GameObject.DestroyImmediate(oldTile);

`

I use this this for loop to destroy objects inside a transform (childs will change dynamically).

Transform[] tArray = transform.GetComponentsInChildren<Transform>(false);
int cCount = tArray.Length;
string origName = "prefabOggetto";

for (int i = 0; i < cCount; i++)
{
	if (tArray *== null)*
  • {*
  •  continue;*
    
  • }*
    _ Debug.Log(“Child name: " + tArray*.name);_
    _ GameObject go = tArray.gameObject;
    if (go.name == string.Format(”{0}(Clone)“, origName))
    {
    Debug.LogWarning(” Destroy: " + go.name);
    DestroyImmediate(go.gameObject, false);
    }
    }*_

Use only on editor.

using System.Linq;

public void DeleteChildren (GameObject badParent) {
	List <Transform> children = badParent.transform.Cast <Transform> ().ToList ();
	foreach (Transform child in children) {
		GameObject.DestroyImmediate (child.gameObject);
	}
}

Similar (and simpler) soultion, but without alocateing new List. Also iterate from last to first sibling (do not require relocate a Array of Transforms):

foreach(var child in transform.Cast<Transform>().Reverse())
            DestroyImmediate(child.gameObject);

Also: “Don’t forget to add the “System.Linq” namespace.”