Containers: Keep It Tidy!
A Lesson on Creating and Utilizing Containers
This is following up on my last article, check out Coroutines with Unity! if you have not already.
This is a quick lesson on creating a container to keep the hierarchy within Unity from being flooded with objects that are instantiated repeatedly or rapidly in quantity.
Create the Container
- Inside Unity, right-click your SpawnManager in the hierarchy window and create an empty object.
- We can simply name this “Enemy Container”
- Attach this to the _enemyContainer component once we create this variable in our SpawnManager script
Write the Code
- Add a new game object called _enemyContainer to your SpawnManager script.
- Then attach the Enemy Container game object inside Unity to the Enemy Container Component on your Spawn Manager within the inspector window.
- Find the _SpawnRoutine method in your SpawnManager script.
- And with one simple line of code, we can now tell the SpawnManager to put our instantiated enemies inside the container.
In the hierarchy window, click the expand or dropdown button on your Spawn_Manager and again for the Enemy Container. When we press Play in Unity, we can now see all the enemies are neatly stored inside the container.
That’s It!
And just like that we have created and utilized a container. This is very helpful for containing any type of element that is going to be consistently instantiated or created in quantities. As well as Items with a limited number of uses that the player can collect and hold on to. Or anything that can add up over time. And just to keep similar or identical objects in a common place. Luckily, creating and utilizing a container is extremely simple and the uses are countless. If only they were all this quick and easy. In my next article, we go From Prototype to Work of Art.