Destroy all Children in Unity

How to destroy all children of a gameobject

Tristan Engel
Jul 15, 2021

We finally added some thrusters to our enemies, but when they get destroyed the play an animation. This prevents us from destroying the game object for visual reasons, until the animation is finished. The thrusters are childs of the enemy game object. When we start of enemy death sequence we use the following code to destroy all children:

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

There are many ways to do this, but this seems to be the cleanest and simplest way without much code.

--

--

Tristan Engel
Tristan Engel

Written by Tristan Engel

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.

No responses yet