In the previous article, we went over how to respawn an enemy when arrives at the bottom of the screen. But, what happens if this enemy was destroyed by the laser or the player? It will not respawn. In this article, I will explain how to continuously spawn enemies every few seconds when the game is running using Coroutine() method.
According to Unity Doc, 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 in the script”.
- Coroutine is very useful when whenever we need to pause/perform a series of steps in sequence before any further action takes a place.
IEnumerator — the best friend of Coroutine
IEnumerator is a return type followed by Coroutine, and we would choose ‘IEnumerator’ over ‘void’ because it allows ending with ‘yield’. ‘Yield’ will tell Unity that we want to wait for x amount of seconds.
For example (based on the above script), an object will be instantiated after 3 seconds. But, this will NOT respawn again because there is no line of code that asks to repeat — That’s when ‘While Loop’ can be used.
While Loop is a loop that will execute a block of code as long as a specific condition is true. This loop can be quite dangerous if the condition is always true which means the loop will never end causing PC to crash, etc. But, in our case, it would work well as we want to constantly respawn while the game is playing.
Now that we have a better understanding of Coroutine, IEnumerator, and While Loop, let’s figure out how to implement them into our script.
- Create a new empty game object called ‘SpawnManager’
- Create a new script called ‘SpawnManager’ and add it to the game object that we just created above.
- In the script, create a new game object variable to get enemy prefab.
- Under void Start(), write ‘StartCoroutine(SpawnEnemies());
- As a new method, write ‘IEnumerator SpawnEnemies()’ that will execute when Coroutine starts.
- To create an infinite loop, write ‘While (true)’. Then, create lines of code to instantiate enemy prefab that will spawn at a random X position from the top of the game screen.
- Add ‘yield return new WaitForSeconds(3f)’ to pause for 3 seconds before resume again.
Great! Now, we have enemies spawning every 3 seconds!
But, the hierarchy window gets messy as we have more and more enemy prefabs being spawned. How can we clean this up?
Tidy Up Spawned Enemies
- Create an empty game object under SpawnManager.
- Create a variable to link this EnemyContainer game object.
- Create a variable ‘newEnemy’ that will store the enemies that were instantiated.
- Write a line of code that will transfer the enemies to EnemyContainer.
Now, let’s see what happens.
Enemy game objects are now stored under EnemyContainer. Looks nice and clean! Awesome!
Stop Spawning when the player dies
We still one major problem — the enemy spawning will NOT end even after the player is being destroyed... So, what’s the solution?
- We can create a bool variable that will check whether the player is dead or alive and pass this onto the while loop condition.
- We would also need to create a method that will be called out when the player is dead and let the while loop know & stop spawning the enemies.
The code will look like below.
Now, in Player script, we need to find a way to link this SpawnManager script, so that we can call out ‘OnPlayerDeath()’ method when the player is dead.
Hmmm, but how do we that…? We can use the logic in the hierarchy.
- First, find GameObject that is called “SpawnManager”.
- Then, get a component called <SpawnManager>.
In code, we will write as below.
Hmm... But, since this line of code is written under ‘void Start()’, we cannot use it outside of ‘void Start()’, can we? In this case, we can create a variable called, ‘_spawnManager’ that will be accessible to the entire script.
Wait a second. Did we miss something? Ah ha! We should perform ‘Null Checking’ just to make sure we avoid any further error or app crash.
Great! Then, we just need to add ‘_spawnManager.OnPlayerDeath();’ just before the player is being destroyed.
Let’s check if it’s working.
lives 3..2..1.. the player is destroyed and the enemy is NOT spawning anymore. Perfect!
Thank you so much for reading this!