How to spawn many objects and keep the Hierarchy panel tidy
Change the Parent of Objects in Unity
Many many enemies in the Hierarchy
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.