Creating Modular Powerup Systems
Creating a modular powerup system can seem daunting, but it relatively simple. We can use if statements to check for which powerup we collected.
We would need to assign each powerup a powerup ID and use the ID to check for collision.
A better way to do this, would be to put our powerup references in an array.
To create an array, we use GameObject []. The square brackets denote the fact that it is an array. An array is a list with a set number of slots called elements. This array will go in our Spawn manager to control the spawning of the powerups.
We can see and manipulate the array in our inspector because of the serialize field attribute. The array we created is empty. We can add an integer value between the square brackets to set the number of elements. We can also add elements to the array as shown below. Add the prefabs for our powerups into the empty spaces.
In our Powerup Script, we need to create a powerup ID variable with a serialized field.
From the inspector, we can assign our powerups their powerup ID.
Before, we were using individual coroutines to spawn our powerups. Now that our array is set up and the references are assigned, we can use one coroutine in the spawn manager to control the spawn of all of our powerups.
Create an integer variable called randomPowerUp. We’ll assign an integer value, randomly, between 0 and 2, every time the variable is instantiated. We will use this variable when calling our array to randomly select an element to spawn.