Modular and Efficient PowerUp System

Photo by Gabriel Crismariu on Unsplash

So far we’ve created a singular Powerup in the form of our Triple Shot. However we want to create a few more and maybe even …a lot more.

So the question is how do we do so modularly and efficiently? Right off the bat I know that I want to create two more Powerups. A Speed Boost as well as a Shield. Not exactly original but classic for sure.

We have the assets and we already have a method in our Player script that detects when the Player collides with the Triple Shots Power Up so let’s extend this functionality to be modular so that we can easily add as many as we need.

There are several ways we could go about this either using tags or an array but for the sake of fundamental knowledge building we are going to go with the tried and true method of using an ID, very similar to a tag in many way but also very easily extendable. In our PowerUps.cs script we’ll create a private int called _powerupID; thinking ahead let’s assign 0 to tripleShot, 1 to Speed and 2 to Shields. We’ll leave a comment to that effect so we don’t forget.

We’ve not created the functionality to the other two Powerups yet but we want the confirm that when the player collides with them that it triggers the correct one. We’ll use our trusty Debug.Log function to send a message to the console telling us so via the following IF statements.

Finally we have to assign our respective Powerup Prefabs to the corresponding ID we assigned in the Powerups.cs script.

Making the _powerupID trigger the correct if statement

Once we run a play test we can see that we’ve successfully triggered the correct powerup.

Next we’ll make the Speed Boost actually give us a boost and change our IF statements to Switch statements. Till then!

Unity Developer — Technology Leader — Making Unity Game Development Look Easy