Creating Modular Powerup Systems (Unity)

Modularity is the name of the game when it comes to good software design and game development is no different. Over the last few weeks I have been working through the GameDevHQ 2D Space Shooter with a modular powerup system in mind. Today I will go over the current implementation of the system and how it can be improved upon. I will also discuss areas where I made slight adjustments to the GameDevHQ design.

In previous articles we talked about creating Powerup prefab assets with animations and sprites. In we looked at creating the powerup and gave it a semi-modular design by using a Powerup.cs script that we would use for all of our future powerups. Here is the current state of that script:

Current powerup script.

The above script differs from the GameDevHQ implementation in that we include a PowerUpID variable and when we call the player we do so with a single call to the EnablePowerUp method that we pass the ID to. We will discuss more about how the player deals with this in an article tomorrow about switch statements.

As you can see the script handles two things, the collision and the movement of the object. It also relies on several other things such as prefab powerups, a reference to the player, and a spawn manager.

Spawn Manager in the inspector

It is this spawn manager that gives this powerup system it’s semi-modular status. The spawn manager has an array of game objects that allows us to plug in the powerup prefabs that we built in earlier articles. We still have to manually build the prefabs, adjust the array length and deal with a lot more in other scripts such as spawn manager and player to get it functioning.

Spawn Manger Array

We can expand on the PowerUp class used by the prefab powerups by adding new properties and methods to them. For example we could add the properties of how they impact the player, how often they are allowed to spawn, or define other specific behaviors we want to modulate. Of course in our semi-modular setup we would have to update the spawn manager and the player scripts to deal with these modifications but you can see how it gives us the power to define a single powerup class that can be used on an infinite number of powerup prefabs.

True Modularity

So how would we make this truly modular? Well C# provides us with all of the super powers of object oriented programing. In order to achieve true modularity we would need to set up an event system to remove the hard coded references to the player. For effective modularity we want code that is reusable and independent that controls small blocks of the game. They should be easily transferable to other games and changes to them should have no impact on other areas of the game.

We could get really creative and use the power of object oriented programming to create inherited classes to setup a more complex system, or we could setup the script to hold variables for the sprites, animations. We can then use lists of the PowerupClass in our Spawn Manager to define the powerups that will be created at runtime. We have plenty of options and as always deciding what you need during game design can save you time during game development.

We may look at making the Space Shooter more modular down the road but for now the semi-modular approach will work. Tomorrow we look at how the switch statement in C# allows the player class to impact the player when a power up is collected.

Turning my passion for video games and 11 years of software development experience into a focus on video game development using Unity3D.

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