Creating Modular Powerup System in Unity

Improving the powerup script to activate the different powerups

Objective: Make the powerup script works with any powerup

I have a powerup script which only activates the triple shot when a powerup is collected. The idea is to use this script for all collectable powerups. In order to do that we are going to create a modular powerup system.

The first step is to declare a new variable called powerupID and we’re gonna determine a value for each powerup in this case 0 is going to be triple shot, 1 speed boost and 2 shields. So after declaring the variable we have to assign the powerupID in each prefab in the inspector windows.

Now we have to change the code in the OnTriggerEnter2D function to check for every powerupID and activate the respective powerup.

And done, as a result the player can activate the correct powerup when it is collected instead of always activating the triple shot.

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.