Creating Modular Powerup Systems

Objective: Figure out how to use just one script for all the power ups.

Triple shot and Speed Power-Ups

Now that I have more than one power-up, I need to decide if I want each power up to have its own script or try to use one script for all power-ups. The answer is one script since all the power-ups have the same behaviour.

To use just one script, we will need to make some changes. The first is to add a variable to store an ID to keep track of the power-ups. I have chosen _powerUpId.

Power-Up Id Variable

I have added the serialized field attribute so I can assign the ID in the inspector.

Assigning the Power Up Ids

Now that we have the ID set for each power-up, we can move on to the logic for calling the appropriate method when the power-up is collected.

For this, I will use if else if statements to check the power up ID.

If Else-If

Below you will see the script in action, when the triple shot is collected the player can shoot the triple lasers and when the speed boost is collected the player moves twice as fast.

Power-Ups in Action

You may be thinking this method could get out of hand if had more than just a few power-ups and you would be correct.

Let’s say you had 10, 20 or even 30+ power-ups, it would be ridiculous to have 30+ else if statements. Luckily there is a better way, I will show you in my next article.

--

--

--

Unity / C# Game developer

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

Recommended from Medium

Flink in a Nutshell

Scalable & Reliable setup of Keycloak as OIDC Provider

One way to independence:-HOW- Part II

Kubernetes API Priority and Fairness

Kanban Part 3 — WIP Limits, Blocked Items and Lanes of Service

Passionate, creative, and solution-driven full-stack developers — Brandcrush needs you!

CS373 Fall 2020: Final Entry

Tableau — Power of Server Repository

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
Bill Rislov

Bill Rislov

Unity / C# Game developer

More from Medium

Day 105: Working with Animation Events in Unity

Building A Wave System

How to Make the Camera Shake Using Impulse Listener Extensions in Cinemachines Virtual Cameras

Creating An Advanced 2D Player Controller Without Any Unity Components! (Part 1)