Spawning The Powerup

Determining How Long Powerup Effects Should Last

We have created our sprite animation. Now we need to make it appear in our game.

Sprite Animation

To do this, lets go to our SpawnManager script. Notice we already have a spawning coroutine for our enamy gameObjects. So we have to make something similar for our powerup.

Enemy Spawning Coroutine

For our powerup we can write something like this.

Powerup Spawning Coroutine

Here we are creating a variable-position spawning variable called posToSpawn. We use this inside our Instantiate. The yield return new WaitForSeconds is fluctuating between 5 and 7 seconds before each spawn. The reason for this range of 5–7 is that if we spawn earlier, it wouldn’t make sense, because the powerup last for 5 seconds. Notice that we are Instantiating a _powerupPrefab so we need to create it as a serialized GameObject variable.

In our Start method we have to call our new coroutine in order to start it. So, right under our calling the enemy coroutine, we call our power up coroutine.


Back in Unity, select the SpawnManager object. Notice that in the Spawn Manager Script component we have a slot that says Powerup Prefab with a None. We need to drag our TripleShotPowerup prefab into this slot.

Now, if we run our game, the Powerup should appear at random intervals of between 5 and 7 seconds in random places along the X axis.



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
Marc Frappier

Marc Frappier

Poultry Scientist by profession, Computer Scientist at heart. After many years in the poultry business, I have decided to give my passion for technology a shot.