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.
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.
For our powerup we can write something like this.
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.