How to manage your spawning objects so they don’t take your whole hierarchy space
In the previous article, I managed to spawn a powerup for the player to catch. But before that I made enemies spawn every 4 seconds from the top.
Everything looked good except when I didn’t destroy the enemies and more spawned, the whole hierarchy was full of enemies.
I needed a solution for this, how I can spawn enemies and keep my hierarchy clean from multiple duplicates of the same object.
So how do we do this? :)
It’s simple really. We create an empty game object and name it “Enemy Container” this will hold our enemies so they don’t take the space in our hierarchy. Now they are hidden inside this empty game object :)
We need to add in our Spawn Manager script a way to add our spawning enemies straight inside this empty game object. The empty game object will be the spawning enemies parent.
Okay so in this code we P.S … Number 4 is where the magic happens! :)
1. See if _stopSpawning is false (If it’s false, our while loop will start).
2. We will create a new Vector3 which is for our area where enemies can spawn.
3. We define _newEnemy GameObject, which is the GameObject we Instantiate (this contains our prefab, position, rotation).
4. Here we set our newEnemy transform to our _enemyContainers transform and parent it.
5. We wait 2 seconds and start to go through our code from the start.
This looks better already! :3
Thank you for reading this, much love! In the next one, I will talk about game prototypes, can’t wait! :)