Day 14: Spawning Objects With Less Clutter
As I mentioned last time, the hierarchy gets way too cluttered when more and more enemies appear in game. If we end up with a lot of enemies on the screen, the hierarchy could be completely filled up and really annoying to navigate. Lets fix that!
First we need to create another empty game object that will hold these enemies. Here we can right click on the Spawn Manager and create an Enemy Container as a child object.
Not it appears underneath the Spawn Manager, and an arrow appears next to the Spawn Manger. If we were to click that arrow, it would hide any of the Spawn Manager’s “children”, which would hide the Enemy Container and it’s children.
Next we need a reference to this new object in the code. We can add a new variable to spawn manager like so.
We also need to assign the enemy container to this script in the inspector.
Now lets modify the actual enemy-spawning coroutine, so that new enemies will be “inside” the enemy container.
We can assign a local GameObject variable to the instantiated enemy in order to “save” it, so we can continue modifying it in the code. On the next line, we can then assign its parent to the enemy container. This means the enemy is now a child of the enemy container, and will show up underneath it in the hierarchy.
As a result, now when the game is played, all the new enemies are placed neatly within the enemy container, so the hierarchy can remain clean.