Unity Basics: Creating Modular Powerup Systems

The most important rule in any coding language is the DRY principle, meaning don’t repeat yourself. If you find that you are using almost the same code repeatedly in your script, try replacing it with a function with an argument. This will be much easier to read and maintain later.

Another problem is having multiple objects in the game that will have very similar or even the same behavior. You should not create a separate script for each object, but just one script and make it modular.

Take a look at this example of the powerup system in Space Shooter game:

Instead of creating a separate script for each powerup, we can create a variable ID and recognize different powerups within that ID. Each powerup uses the same movement and trigger function, with a custom ID we can add different additional behaviors to different powerups.

If you code, always think modularly.

--

--

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
Damian Dąbrowski

Hi, I’m Damian, a Unity Software Engineer who loves building education and simulation games.