How to spawn many objects and keep the Hierarchy panel tidy

Change the Parent of Objects in Unity

Tristan Engel
2 min readMay 9, 2021

Many many enemies in the Hierarchy

Before: mess. Enemies running wild without a parent to keep them in check.

In the gif above you see a lot of enemies spawning and filling up the Hierarchy panel. When you have a more complex game this makes it a lot harder to debug or test design choices.
Today we create a container to put all those enemies in and keep the Hierarchy panel tidy.

Assign a parent to a game object

We created a enemy game object called “Enemy Container” under our “Spawn_Manager” game object. Next we created a reference (enemyContainer) in our script with the Inspector to the “Enemy Container”.

//spawn enemy
GameObject newEnemy = Instantiate(enemyPrefab);
//set parent to container
newEnemy.transform.parent = enemyContainer.transform;

With GameObject.transform.parent we get our current parent of our freshly spawned enemy. Next we change the parent to become the enemyContainer.transform. The ‘.transform’ part is needed because the type of parent is a transform.
Let’s see the result.

After: clean. A parent that keeps the enemies grouped.

--

--

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