Spawning Objects in Unity Without the Clutter
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:
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:
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:
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.