Powerup Series: Extending Functionality to Other Powerups

Part 5: Maximize your powerup code with modularity

Erin Kerr
2 min readNov 3, 2022

Typically, when there is one powerup, there are usually more to follow. Let’s take a look at how to re-use the code you’ve written to create new powerups!

The Powerup script

All powerups have more similarities than differences, if you think about it. A powerup needs to move down the screen and have trigger collision logic that recognizes the Player. The main difference between the Triple Shot Powerup and any other powerup you may add later is the functionality it triggers in the game. For the Triple Shot Powerup, the Powerup script only needed to call the Activate Triple Shot method — all the functionality is actually housed in the Player script. So, the Powerup script would just need a way to know what type of powerup it is attached to, so it can call a different method activate the different changes in functionality.

Adding an Powerup type indicator

An integer variable to hold the Powerup ID

To track what type of powerup object is attached a particular Powerup script, we can use a simple integer variable to hold an ID number. In the code comments, be sure to indicate what each value stands for (ie. 0 is Triple Shot). Be sure to serialize the field so you can assign the Powerup ID to each Powerup prefab in the Inspector.

Checking the Powerup ID

Add an if/else if to check the Powerup ID

Inside the OnTriggerEnter method, after identifying that the Powerup has collided with the Player, check the powerup ID with an if/else if statement and call the corresponding method to activate the powerup’s functionality.

Sample gameplay using a modular powerup script

And that’s really all there is! Add as many powerups as you can think of and just be sure to update the Powerup ID on each Powerup prefab so you can activate different behaviors.

--

--