Creating Modular Powerup Systems | Unity Developer

Derek Anderson
2 min readJun 2, 2023

--

The triple shot powerup isn’t going to be the only powerup in our space shooter game. We’re going to be adding some other powerups, but we don’t need to add a new powerup script for each additional powerup. We can create one powerup script that will define and control multiple powerups with their own behaviors. This will be our modular powerup system.

To accomplish this, we’ll start with else if statements. There is a more effective way to accomplish this, but that will be covered at a later time. The console messages will take place of the speed and shield powerups being collected and active for the time being.

As we’re dealing with powerups, all of our work will be in the powerup script. We can create a global variable to keep track of the powerup that has been picked up. Here, the powerups will be determined by powerupID. SerializeField will allow us to change the value in the Inspector in Unity. As an example, our speed powerup pickup will be assigned an ID of 1, which we can set in Unity.

This is where the if else statements come into play. Now that the ID’s are assigned, we can write out the logic for what happens when we collect the powerup.

if (powerupID == 0)
{
player.tripleShotActive();
}
else if (powerupID == 1)
{
Debug.Log("Speed powerup active");
}
else if (powerupID == 2)
{
Debug.Log("Shield powerup active");
}

Here is the logic I have written in my collision code. If I collect the triple shot powerup, which has an ID of 0, then the triple shot will be active on the player’s ship. If I collect the speed powerup, which has an ID of 1, then the speed powerup will be active on the player ship. At this point, the speed powerup hasn’t been implemented quite yet, so I had Unity print out a message saying that the speed powerup is active.

This will suffice for now, but when in the case of multiple powerups, a switch statement will be more effective, which we’ll touch on next.

--

--