Objective: Avoid clutter inside the hierarchy of the scene in Unity.
Continuing with the previous post about how to spawn enemies infinitely using coroutines, we need to get rid of clutter inside our scene in Unity so that it doesn’t make the hierarchy look like a mess.
For example, if we run the last version of the last post we will see that when enemies are spawned in the scene the hierarchy looks like this:
So, in order to avoid the above, we can hold all the enemy clones using an empty gameobject inside the spawn manager:
Once created, we need to create a new variable inside the spawn manager script (where the clones are created) to have the reference of the empty gameobject in the scene:
Now save the script, go to Unity, select the spawn manager and then drag the empty gameobject into the variable of the script attached to the spawn manager:
Finally, open the spawn manager script and edit the next lines inside our coroutine:
As you can see, we need a Gameobject variable to save every clone spawned in the scene, so that every time this happens the empty gameobject gets set as the parent of the clone.
Now the clones look like this in the hierarchy and the clutter is gone:
And that's it! the clutter won’t be an issue as long as we keep the hierarchy items organized. I’ll see you in the next post, where I’ll be showing how to take your game in Unity into the next phase with new assets.