My journey becoming a Unity game developer: Coroutines with Unity. Activating the Triple Shot after the player collects the Triple Shot Powerup

Rhett Haynes
5 min readJul 20, 2022

Objective: When the player collides with the Triple Shot Powerup, the player will be allowed to shoot Triple Shot lasers for 5 seconds using a coroutine. After 5 seconds, the player will go back to shooting single lasers until collecting another powerup. Also, destroy the parent game object of the Triple Shot to make sure the Triple Shot lasers are not active in the game when they go offscreen or destroy enemies.

Triple Shot lasers destroyed with their parent objects.

First thing we need to do is create a new variable that will have an Access Modifier Private, Type Bool name isTripleShotActive. This variable will check to see if it’s set to True to shoot Triple Shot Lasers, or set to False to shoot regular lasers from the player. The isTripleShotActive variable will be set to False by default. Also, we use the SerializeField attribute just for testing purposes in the Inspector, but it won’t be needed in the game’s final production.

Changed tripleShotActive variable from Public test to a Private SerializeField.

Before creating the Triple Shot Active() method, change the isTripleShotActive variable to the industry standard private identifier which has an underscore in front of the variable’s name like so (_isTripleShotActive).

To change the isTripleShotActive variable from False to True, we need to create a method name TripleShotActive() that has a Public Access Modifier. It’s best practice to change a Bool Type variable inside of a method as it’s easier to control its change from True to False and vice versa. Inside the TripleShotActive() method, set the isTripleShotActive variable to True giving permission for the Triple Shot to be turned on.

TripleShotActive() method created to set isTripleShotActive variable to True.

Now we want to turn off the Triple Shot being active after 5 seconds when the Player is using this special shot inside of the TripleShotActive() method. To do this, we will use a Coroutine to turn off the Triple Shot being active. In Unity, a coroutine is a method that can pause execution and return control to Unity, but then continue where it left off on the following frame.

To create a Coroutine, we need to use the IEnumerator() function to yield or pause executing tasks and name it TripleShotPowerDownRoutine(). In the TripleShotPowerDownRoutine(), we will Yield or pause execution of the next task, return a new WaitForSeconds of 5 seconds suspending the coroutine execution for the given amount of seconds before switching back to the regular single shot by setting _isTripleShotActive to False.

Back in the TripleShotActive() method, we need to use the StartCoroutine() function to actually use a coroutine which will be the TripleShotPowerDownRoutine() being passed in. In most cases you want to use the coroutine name without the “quotations” around the name. However, using StartCoroutine with a string method name allows you to use the StopCoroutine() with a specific method name. The downside of using the string name version is that it has a higher runtime overhead to start the coroutine and you can pass only one parameter.

TripleShotPowerDownRoutine() created to wait 5 seconds, then deactivate the Triple Shot.

In the EnemyBehavior script inside the OnTriggerEnter2D() function, we will use script communication to get the PlayerBehavior script component from the Player object. Under the IF condition checking if the object colliding has a “Playertag, create a new local variable with Type PlayerBehavior name player to get the other object’s transform, and Get the Component name PlayerBehavior. Check IF the player’s component is not NULL, and if it isn’t we want to use the player variable to use the Damage() method. Also, we want to Destroy this enemy game object that collided with the Player.

Used script communication in the EnemyBehavior’s script to access the PlayerBehavior’s component.

One other problem we need to fix is that the Triple Shot has a Parent game object to the 3 children lasers. These parent objects need to be destroyed when they go offscreen. Inside the IF statements checking if the lasers are above 8f on the Y-axis, we need to check if the Triple Shot’s parent transform isn’t equal to NULL. If not, destroy the transform of the parent game object. If it’s a regular laser fired, just leave the previous Destroy(game object) code as is.

When we play the game, notice the Triple Shot’s parent objects being destroyed when they hit enemies and when they go offscreen in the Hierarchy.

Triple Shot lasers destroyed with their parent objects.

Coroutines can be very effective in certain situations like when we used them to have a delay between each laser shots. We set _canFire to True inside the Start() function to start the game. Then turned it to False inside the FireLaser() method after each shot, but turned it back on after 0.3f seconds waiting for the next shot. Finally, inside FireLaser() method we StartCoroutine(LaserDelay()) after _canFire is set to False after every shot.

Using a coroutine to delay lasers fired every 0.3f seconds.

--

--