Spawning Objects in Unity Without the Clutter

Micha Davis
2 min readApr 8, 2021
We need some elbow room.

In the last article I made a spawn manager object and scripted it to plop out an enemy every five seconds. Now, my hierarchy looks like this:

Animation of Unity hierarchy window slowly filling with cloned enemy gameobjects

The longer the game goes on, the more cluttered this gets. We need to clean this mess up and keep it tidy so we can always see what’s going on. I’ll start by creating a new empty game object called “Enemy_Containter” and dragging it into the Spawn_Manager. Then I need to script out assigning new enemies to this container as child objects.

The code for this is pretty simple:

Add a variable declaration.
The modified coroutine.

First I declare a global variable of data type GameObject for the enemy container and serialize it. Next, in the coroutine I declare a local variable of data type GameObject called “newEnemy” with the instantiation function included. Finally, I assign the new enemy as parent to the enemy container through each object’s transform component.

Going back to Unity, I let the Spawn_Manager know which object is the enemy containter on the Inspector. Now, when I run the game we see this in our hierarchy:

Animation of the Unity hierarchy window with enemies falling neatly into the enemy container game object.

Oh, yeah. That’s much better.

I’d say that’s a complete prototype. So let’s graduate our project to a fully realized game. In the next article I’ll address converting from a prototype to a work of art.

--

--

Micha Davis

Unity Developer / Game Developer / Artist / Problem Solver