Unity Development — Modular Powerups

Christopher Graf
3 min readMay 24, 2021

--

We have discussed how powerups can enhance your game. If you have done your balancing with care, there is a chance you have made more than one powerup. Variety in a game is always a good thing. The player is obviously going to be able to collect all of these powerups. You’re not going to use a different script for this logic on of each one. Let’s see how to make this process modular.

For this to work, you are going to have to make sure that each powerup is set up as a prefab already. They also should share the same Powerup script. Once that is done, this is actually very simple. Let’s take a look at that Powerup script now.

By now you know how to set up trigger collisions. An important part of this step is also making sure you know with what you are colliding. GameObjects have tags, which you can set up at the of the inspector when selected.

In OnTriggerEnter(), OnCollisionEnter(), or their 2D counterparts, you can check the collider by using CompareTag().

Inside that IF statement, you’ll know that everything you do is in relation to the player now, based on that assigned tag. We are now going to use some more IF statements and make this a modular system. First, go to the top of the script and make a serialized int variable.

This variable is going to help us separate which powerup the player has collected. Go to each prefab and in the inspector, set up a different int value for each powerup. Make a note somewhere, so you will remember which value is for which powerup.

Now, in your OnTriggerEnter2D() and inside your CompareTag() IF statement, create another IF statement. This one should check the value of that int variable.

All you’ll have to do is separate with an ELSE-IF and use that int variable and you could use the same script to represent 100 powerups! It is that simple.

--

--

No responses yet