Destroy All Children of Object

Hello,

I can’t get my head around this, I have a game empty called TextHolder. At times, this TextHolder is populated with text objects (via Instantiate), as children. I want to be able to get all the children (text objects) of the TextHolder and Destroy them when needed.

I don’t want to cache them, as in adding them to an array whilst I Instantiate them, I want to be able to search the ‘TextHolder’ object for any children, and Destroy them:

var allChildren = TextHolder.GetComponentsInChildren(Transform);
for (var ac = 0; ac < allChildren.length; ac ++){ //var child : Transform in allChildren
    print("Removing old text modules");
    Destroy(allChildren[ac].gameObject);
}

Although this doesn’t actually do anything, its as if the TextHolder has no children.

What am I doing wrong here?

Hmm. Your code is too complex. I even don’t know what it is intended to do. But maybt you should try transform instead of Transform
Here is more compact working code.

foreach (Transform child in TextHolder.transform) {
	GameObject.Destroy(child.gameObject);
}

If TextHolder is an instance of an object, not class. If you want to simply remove all children of current object you can write

foreach (Transform child in transform) {
	GameObject.Destroy(child.gameObject);
}

or you can write extension method for Transform class and call it on objects transform you want to clear

public static class TransformEx {
	public static Transform Clear(this Transform transform)
	{
		foreach (Transform child in transform) {
			GameObject.Destroy(child.gameObject);
		}
		return transform;
	}
}

in your code myObject.transform.Clear();

in JS:

var childs : int = transform.childCount;

for (var i = childs - 1; i >= 0; i--)
{
    Destroy(transform.GetChild(i).gameObject);
}

in C:

int childs = transform.childCount;

for (int i = childs - 1; i > 0; i--)
{
    GameObject.Destroy(transform.GetChild(i).gameObject);
}

So you create a tag just to delete your Objects like I did. But, like @frogsbo said it: work perfectly. Just don’t forget to add = at i>0 just like @Thompsan mentioned.

    int childs = clone.transform.childCount;
    for (int i = childs - 1; i >= 0; i--) {
        GameObject.DestroyImmediate( clone.transform.GetChild( i ).gameObject );
        }

Because my objects are loaded via a Coroutine, the above examples as well as the typical approach of getting all GameObjects with a tag (using ‘FindGameObjectsWithTag’), iterating over them and removing them, I used the following approach:

  public IEnumerator DoDeleteAll()
        {
            while (Holder.transform.childCount > 0)
            {
                var items = GameObject.FindGameObjectsWithTag("YOUR_TAG");
                foreach (var item in items)
                {
                    Destroy(item );
                }
                yield return new WaitForSeconds(0.001f);
            }
        }

For posterity I wanted to add here what worked for me:

var children = new List<GameObject>();
foreach (Transform child in transform) children.Add(child.gameObject);
children.ForEach(child => Destroy(child));

I got that code from this forum post:
https://forum.unity.com/threads/deleting-all-chidlren-of-an-object.92827/

Only FabDynamic’s answer works. If you iterate and delete at the same time, it won’t - you’ll be left with children.

public static class GameObjectExtensions {
  public static void DestroyChildren(this GameObject t) {
      t.transform.Cast<Transform>().ToList().ForEach(c => Object.Destroy(c.gameObject));
  }
 
  public static void DestroyChildrenImmediate(this GameObject t) {
      t.transform.Cast<Transform>().ToList().ForEach(c => Object.DestroyImmediate(c.gameObject));
  }
}

$$anonymous$$ is $$anonymous$$ going to hier the $$anonymous$$?

or you can write extension method for Transform class and call it on objects transform you want to clear

public static class TransformEx {
public static Transform Clear(this Transform transform)
{
foreach (Transform child in transform) {
GameObject.Destroy(child.gameObject);
}
return transform;
}
}