Modularizing Power Ups

In a previous article, you learned how to add a power up to your game. Most likely, if you’re adding one power up, you are going to add more (once you power-up, you just can’t stop). Let’s take a look at how to streamline your power ups by reusing a single Power Up script.

To set up the initial power up, you needed a script to manage the behavior of the power up — not the functionality it adds to the Player, but the way it moves down screen and how it collides with other objects. When creating additional scripts, much of that behavior will stay the same. So why not make use of the script you already have?

Add a Power Up ID to the script variables

You can associate a power up ID to a type of power up, which allows you to track which power up is using the Power Up script at any particular time.

Assign a Power Up ID to the Prefab for each power up

In the Unity Editor, select each power up prefab and assign an Power Up ID (ie. 0 to the Triple Shot power up and 1 to the Speed Boost power up).

Since the only thing that changes from power up to power up is what method it calls on the Player, we can specify which Power Up ID is associated to which method.

Update collision method to check for power up ID before calling player functionality

Within the collision method, check the Power Up ID and only call the method associated to that type of power up. With these changes, you can assign the same Power Up script to each Power Up you add — just assign a new Power Up ID and add a check for that ID in collision so it corresponds to the correct functionality.

Play-testing Triple Shot and Speed Boost power ups powered by a single Power Up script

Now, we have more lasers and more speed! What else can we do with power ups?




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Communication Protocols

Starting with Solong

Dependency Injection


26. Remove Duplicates from Sorted Array

Troubleshooting HTTP 502 bad gateway in AWS EBS

Kafka Consumer Group Experience

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
Erin Brown

Erin Brown

More from Medium

How to stick decals on the Spatial Mesh of Hololens2

The Ultimate Guide To Start Making 3D Video Games

How we made a roguelike game for the Commodore 64

Setting player boundaries