Creating Modular PowerUp Systems

Ryan McCoach
3 min readApr 8, 2021

In my last article, a Triple Shot Power Up was created and with this a PowerUp script to control the positioning and movement of the Triple Shot PowerUp Game Object in the game scene. When the player collects the PowerUp, the TripleShotActive is called from the player script to allow the player to shoot three lasers instead of one.

I want to add at least two more PowerUps, Speed Boast & Shields, to my game. I could create a script for each of the PowerUps, but that would be redundant since the behavior of all of the PowerUps will be the same. The only thing that will be different is the ability each PowerUp will give to the player. This means one script will be responsible for all of the PowerUps behaviors, whether I want to add 50 or 100 of them into the game.

In Object-Oriented Programming, this is called modular programming. It is the idea of breaking down complex programs into smaller components which handles the desired functionally of that object.

In the PowerUp script we are going to create a int variable that will be the ID number for each of the powers with 0 = Triple, 1 = Speed Boast and 2 = Shields. Add a SerializedField attributes so we can assign each of these ID numbers to the respective PowerUps.

TripleShot ID
Speed Boast ID
Shield ID

When each PowerUp is assigned their ID number, we just need to create a conditional to check to see which one the player collected and call that matching PowerUp method from the player.

--

--