Creating a Spawn Manager Part2: Spawning Objects in Unity without the Clutter

Joe

--

Currently in the Hierarchy things can get quite “busy” as the number of enemies spawned increases. This will become more of an issue as other things, like power-ups, are added to the scene. To help with this lets do a bit of housekeeping on how gameobjects are organized in the Hierarchy view. This will allow the location of assets in a quicker manner and aid in debugging some types of issues.

Filling up the Hierarchy window

To do this lets create an empty gameobject and call it Enemy_Container. The Enemy_Container will hold all the Enemy gameobjects and keep them organized and in one place. To make this happen a few lines of code will need to be added to the SpawnManager script.

First the script will need to know that the Enemy_Container exists so lets declare a variable for it so that the empty gameobject Enemy_Container in the hierarchy can be linked to the SpawnManager script in the inspector.

Create the variable to link the Enemy_Container to the SpawnManager script
Connect the Enemy_Container to the new Enemy Container spot on the SpawnManager script

At the same time lets drag the Enemy_Container gameobject on to the Spawn_Manager gameobject so that it is nested inside the Spawn_Manager.

Drag the Enemy_Container onto the Spawn_Manager

After that the gameObjects parent can be assigned in the code. To do this we cannot use the code as is. Currently the Enemy prefab is Instantiated and does its task, but to assign it to be organized under the Enemy_Container access is needed to components like the transform of the gameobject. This can be done by changing the Instantiate call to a GameObject assignment, similar to assigning a variable. Lets create a newEnemy GameObject and assign the Instantiate call to it, like this

Assign the Instantiate call to the newEnemy GameObject

Now the transform can be accessed along with other components by using the newEnemy variable. Now the parent of the Enemy can be set to the _enemyContainer. As the parent attribute is of type transform the transform component of the _enemyContainer must be used like this

Assign the parent of the newEnemy

Now as the Enemies spawn they are all nested within the Enemy_Container keeping the Hierarchy nice and organized

Organized Hierarchy

The next thing to clean up on the Spawn Manager is the spawning of Enemies. Currently the Spawn Manager will spawn enemies until the wave is done, but what if the Player has died? It does not make sense to continue to spawn Enemies after the player has died. So lets add some logic to do that.

First in our SpawnManage script lets add a Boolean variable called _stopSpawning and initialize it to false. The logical check to use this variable will be added to the while loop. As I have setup my Spawn Manager to spawn in waves I will need to add this to the logic check. This means that the Spawn Manager will continue to spawn Enemies for me as long as the _numberOfEnemies is greater than 0 and _stopSpawning is false. If either of these checks are not satisfied then the Spawn Manager will stop spawning enemies.

Modified while loop to check for player death and stop spawning

Next a public method will be setup to access this so that when the Player script detects the players death it will be called and change the _stopSpawning to true.

Method to change _stopSpawning value to true

To get this to work now, the Player script needs to be able to communicate with the Spawn Manager. So in the player script a SpawnManager reference will need to be created. To start at the top of the script, above the Start method, lets make a SpawnManager variable named spawnManager like this

Create a variable to store the reference of the SpawnManager

Next inside the Start Method the new _spawnManager variable needs to be assigned by using GetComponent<> as it needs access to the SpawnManager script component from the Spawn_Manager GameObject. To do this the the Spawn_manager GameObject must be found and if found the component we want needs to be chosen. This can be accomplished like by the following

Assign the _spawnManager

For debugging purposes anytime a reference like this is made it should be followed with a null check, just in case something goes wrong it can be logged so the problem is recorded. Lets do that like this

Null check on the _spawnManager reference

Once this is done we can go to the Damage() method in the player Script and right before we destroy the Player we can call the OnPlayerDeath() method from the SpawnManager script like this

Call OnPlayerDeath()

Now the Enemy spawning will stop after the Player has died. I also added a quick check in the Enemy Script to check if the Player exists and if it does not then instead of respawning existing enemies it will destroy them after they leave the bottom of the screen.

Stop enemy spawning after player death

--

--