Creating Modular Powerup Systems
Setting up a System for Multiple Powerups
3 min readMay 20, 2022
Now that I have a working Triple Shot Powerup, I want to add more powerups. My next powerup will be a Speed Boost Powerup.
- The first thing I had to do was drag the Speed Boost Powerup sprite from my “Sprites” folder into my Hierarchy panel, rename it to “Speed_Powerup”, set the “Scale” to “0.5, 0.5, 0.5”, and set the “Sorting Layer” to “Foreground”.
- I added a collider component to the sprite, scaled the collider to the sprite, and made sure to place a checkmark in the “Is Trigger” option. I used a Circle Collider 2D, but a Box Collider 2D would have worked just as well.
- I added a Rigidbody 2D component to the sprite and made sure the Gravity Scale was set to “0”.
This powerup behaves very identical to our other powerups, so creating a new script wouldn’t be very productive. Because of this, I will use the same “Powerup” script and make some changes to the script for each powerup I add to the game.
- I dragged my “Powerup” script from the “Scripts” folder onto my “Speed_Powerup” game object.
- I then dragged the “Speed_Powerup” game object into “Powerup” folder within the “Prefabs” folder to make it a prefab.
- Next, I created another private variable inside my “Powerup” script named “powerupID” and added the [SerializeField] attribute so I could assign it in the Inspector panel. This variable will hold an integer for each type of powerup.
- I changed my “Powerup” script to include a switch statement inside the OnTriggerEnter2D function to check which value was passed into the “powerupID” variable. With the value, I am able to run the corresponding powerup code. I used Debug.Log to add a third powerup named “Shields”. What space shooter game wouldn’t include a shield powerup?
Note: Always run a NULL check to make sure the component is valid.
A "default" is always good practice, in case a value is passed
that isn't assigned.
- I went into the Inspector panel for both powerup prefabs I created, and I assigned the proper values for the “powerupID” variable.
- I dragged both of my powerups from the Prefabs folder onto the Scene window for quick access to them and ran the game to test that all switch cases were working.