Day 9: Spawning More Enemies!

Right now we only have 1 enemy. When we destroy it, its just gone. Now what? Eventually we will have a game with a score board and several enemies spawning all the time. Maybe even some boss levels? For now lets figure out how to get more enemies to spawn. We’re going to do this by using a Coroutine.

“A coroutine is like a function that has the ability to pause execution and return control to Unity but then to continue where it left off on the following frame.” -Unity Documentation

Lets create a new script called SpawnManager and a new empty Game Object called Spawn_Manager.

We need to declare the coroutine like this:

Coroutine for spawning enemies at a random place on the x axis.

“It is essentially a function declared with a return type of IEnumerator and with the yield return statement included somewhere in the body.” — Unity Documentation

The yield return new WaitForSeconds line is the point at which execution will pause and be resumed the following frame. To set a coroutine running, you need to use the StartCoroutine function. We will call this in our Start function since we want enemies to spawn at the beginning of our game.

The Vector3 we have in our coroutine is setting a random x position for our newly spawned enemy and making sure it spawns above the screen edge. We’ll get into the other 2 lines in a second. Comment those out for now.

Be sure to attach this script as a component to your Spawn_Manager Game Object. Run the game and you should see several enemies spawning now!

If you look over in the inspector you’ll see that when there are several enemies spawned the inspector starts to fill up with enemy game objects. This is just messy. SO we need to not only keep our code nice and clean, but we need to keep the inspector clean as well. Lets make an empty game object called Enemy_Container and child it to our Spawn_Manager.

simply drag and drop the enemy container onto the spawn manager and it will child it.

The 2 lines of code we commented out are handling this. Lets break it down.

We need a variable to store our _enemyContainer; So declare it above:

[SerializeField] private GameObject _enemyContainer;

now drag the enemy container game object into that area on the SpawnManager component on the Spawn_Manager. We want the newly instantiated enemies to automatically go inside that container when they spawn to keep our work environment clean. SO!

GameObject newEnemy = Instantiate(_enemyPrefab, posToSpawn, Quaternion.identity);

newEnemy.transform.parent = _enemyContainer.transform;

These two lines help us access the parent of the newEnemy and assign it to the enemyContainer transform. Now when we run the game our enemies will spawn inside the enemy container.

I dont know if you’ve died yet while playing your game, but if so… you may have noticed that the enemys keep spawning. We want them to stop spawning. SO we need to have a variable called stopSpawning of type bool. Then say

while(stopSpawning == false) in our coroutine. We need to create a method in our Player class that helps set this bool to true and then perform a task. In our Player class create a private SpawnManager spawnManager; variable. In the start function we need to get this component. Lets define what our spawn manager IS and then get the SpawnManager component.

spawnManager = GameObject.Find(“Spawn_Manager”).GetComponent<SpawnManager>();

now in our Damage() method say

spawnManager.OnPlayerDeath(); before you destroy the gameobject. This accesses a function we have not created yet.

In our SpawnManager script we need to have an OnPlayerDeath() function that we set our stopSpawning to true in. MAKE SURE ITS PUBLIC.

Now when you die, the enemies stop spawning new enemies! However many enemies were spawned at the time of your death will remain though. We will fix this later when we implement a “Game Over” screen.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store