Cleaning up the Hierarchy — Creating an Enemy Container

As you saw in the previous article, spawning Enemies is fun, but it gets messy in your Hierarchy if left as is. So it’s best to create an Empty Game Object to contain all the Enemy Spawns, a container if you will.

Create The Enemy Container

Creating a handle for our _enemyContainer.

First, inside our Spawn Manager script, we’ll create the handle to get a reference to our Enemy Container within our code. We’ll link it in the Inspector.

Next we’ll create an Empty Game Object as a child to our Spawn Manager — a logical place to put our container. You could leave it as a separate Game Object if you want, but I feel it makes sense to put it with the Spawn manager because it is controlling where the Spawns are situated in the Scene Hierarchy.

Then, link it with our Spawn Manager script.

Creating our Enemy Container — An Empty Game Object.

The code

Inside our SpawnRoutine(), within our SpawnManager script, is where we’ll set our spawns to be grouped in our Enemy Container. We do this by setting it’s parent.

But first, in order to tell our new spawn where to be a child, we have to create a reference to that spawn.

So in front of our previous Instantiate code, we can create a newEnemy variable of type GameObject.

Now, we can tell newEnemy that it’s parent will be the Enemy Container! This is done by typing newEnemy.transform.parent (the instantiated enemy’s parent) = _enemyContainer.transform (is equal to the _enemyContainer).

Spawning in our Enemy Container!

Now look how clean and organized that looks! If we have a level with a hundred enemies in our Scene, they can be neatly organized, and collapsed at will!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Frank Warman

Audio Engineer turned Unity Game Dev. Will be combining both my skillsets when appropriate, and will be documenting my Unity Dev growth in these Medium Articles