Creating Modular Powerup Systems In Unity

Samantha Gittemeier
3 min readMay 24, 2023

--

Games with powerups tend to have several different powerups with different capabilities that they give to the player. If we have 100 different powerups, do we want to give each powerup it’s own script? We can, but it would be chaotic and messy. What we can do instead, is have one powerup script that is used for each powerup, and inside that script, created a modular powerup system so we can re-use the same script, but still have our system differentiate between the different powerups and therefore activate the correct abilities.

In this case I have a 2D space shooter that has a triple shot powerup and a speed powerup. Rather than making an entirely new script for my speed powerup, I’m going to re-use my powerup script that my triple shot powerup uses. My triple shot powerup and speed powerup will function almost the same aside from what it will do for the player, and that is where I will create a modular powerup system.

First I’m going to start by creating an ID for each powerup that I will have. I can do this by creating an int variable in our script and labeling it as the powerupID. I’ll make it so that 0 (all numbers start at 0) is my Triple Shot powerup, 1 is my Speed powerup, and 2 is my Shields powerup that I’m going to implement later on.

Now in the Inspector, since the int is a [Serialize Field] I can still see and change it in the Inspector, and I can now change the PowerupID for my Speed powerup to be 1 and leave the PowerupID at 0 for my Triple Shot powerup.

And now in the method where I am telling my powerup script to call for the Triple Shot action, I can create new if and else if statements to tell my system to check for the PowerupID. I can then tell my system that if the PowerupID equals 0, call for the Triple Shot ability, and if it is 1, call for the Speed ability, and if it is 2 then call for the Shields ability.

Now I have created a modular powerup system so that I can continue using one script for every powerup, but still be able to identify and activate the right ability for the right powerup. Now that you’ve learned to do this, see if you can program one script, to call for 2 or 3 different methods with 2 or 3 different powerups.

Bonus: You may be wondering if there is an easier way to do this rather than using a bunch of different if and else if statements, as it can get just as messy having 100 different if and else if statements, than having 100 different powerup scripts. In my next article I will be talking to you about using Switch statements to make this cleaner and more efficient.

--

--