Game Dev Blog — Spawning Objects in Unity without the Clutter
Don’t be a litter bug.
In yesterday’s article, I demonstrated how to create a spawn manager to create a continuous stream of new enemies to destroy. I know all of you are great at video games and no enemy would last more than a few seconds before you mercilessly blasted them to space dust, but no everyone is that good.
Because of the likely hood that there might be several enemy fighters and dozens of laser bolts from both sides active at times during the game, the Hierarchy view can get quite cluttered. So today, I am going to show you one way to clean it up as the objects are spawned.
The steps are as follows:
- Create a container for the objects to go into.
- Instantiate the object.
- Grab the reference to the object.
- Then lastly, assign the parent of the object to be the container we setup to use.
First we need to create our container. I put mine as a child object of the spawn manager and added a GameObject variable in the script to hold the reference to this container.
As the code stands now, we do not have any reference to the game object once it is instantiated.
That will not work for what we are trying to do. So we need to store it in a temporary variable of type GameObject instead.
Now we can assign the ‘Enemy Container’ as the parent of the game objects as they are instantiated.
In the video below I have disabled the player game object and set the spawn rate to 1 second between spawns to help show how this method keeps the Hierarchy window uncluttered.
With a game prototype this simple, it might not seem like a big deal to have 5 or 10 enemy game objects in the Hierarchy window. However, in larger projects you might have dozens or even hundreds of game objects and scrolling through to find the one you need to check on would be more difficult if they are nor organized.
So there you go. Spawning multiple objects and putting them under in a container object. Easy-peasy.
Tomorrow is a big day! I have the basic game play working as planned so it is time to bring in the actual game assets to replace the mock-ups we have been using. Here is a preview…
See you there!