Project 1: Space Shooter
Organizing object spawning in Unity
And finding a way to end infinitely spawning enemies
While SpawnManager works perfectly there are still two lingering issues with it. The first one being the never ending spawning of enemies even though the player is dead. The other one is how the Hierarchy view fills with the spawned enemies. It’s not a huge problem for me now, but in the future when I’m spawning all sorts of objects it could lead to a situation where the Hierarchy view is showing an endless list of one type of object.
So first things first, let’s have a natural end to my spawning loop. Because my Player script holds the information about the players health, and the eventual destruction of the Player object, it would be a great idea to add a mechanism here which could tell the SpawnManager to stop spawning enemies.
But how to do it? Since the question is “is the player alive?”, which is a “yes” or “no” type of question, I could then use a bool type of variable in the SpawnManager script to indicate whether or not to stop spawning, I name it _stopSpawning. Very clever…
Now, I don’t want anything outside of the SpawnManager script to change its value, so I set it to private. But I still need a way to change its value, for this to happen I make a method called OnPlayerDeath, the only job that it has is to change the _stopSpawning from false to true, so public void is all I need. It has to be public so that I can call it in my Player script.
The tricky part comes next, because how do I call it inside my Player script? I already know that GetComponent method will give me access to the OnPlayerDeath method, but last time I used it, I had a reference to the GameObject which method I wanted to use. This time I don’t have it. Looking at the manual entry for GameObject, there is a method called Find. Reading the method’s description it can find a game object and return it or null, if it can’t find it.
Using this information I make a private variable for a SpawnManager script as _spawnManager. Then at the Start method I find and save the SpawnManager script into the _spawnManager. If for some reason this script cannot be found I have to do a “null check and produce an error message. But if it does work I can call the _spawnManager script inside my Player script’s Damage method, and access the OnPlayerDeath method when the health of my player reaches zero.
Then all I have to do is to change the while-loop’s argument from true to
_stopSpawning == false, so that when the player dies _stopSpawning changes into true, which causes the argument to evaluate to false, which then will end the coroutine. Simple, now that I did it.
Now to the easier task. First thing that I have to do to organize my spawning is to right click on my Spawn_Manager object, select Create Empty from the list, which will create a new empty GameObject as a child object for my Spawn_Manager object, and name it Enemy Container. After that I have to make a private GameObject in the SpawnManager script, called _enemyContainer, make it visible inside Unity by adding [SerializeField] on top of it. Then in Unity drag the Enemy Container to this field.
After that in the SpawnRoutine I will insert every new enemy into a variable called newEnemy and add the transform container as a parent container for every enemies’ transform component. This will neatly save every new enemy inside the Enemy Container, which then can be collapsed to hide all the spawned enemies, so that they don’t fill the whole Hierarchy view.