Creating Modular Powerup Systems

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”.
Adding Collider2D & Rigidbody2D to Powerup

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.
Creating Speed_Powerup 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.
Create powerupID Variable
  • 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.
Assign Value to powerupID in Inspector Panel
  • 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.
Test powerupID Switch-Case Functionality

Thank you for taking the time to read this article. Follow me to see updates of the 2D Space Shooter project.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
James Hills

James Hills

I am a married father of 5 children. I decided at 13 years of age that I was going to become a Software Developer.