Creating Modular Powerup Systems

Austin Hall
2 min readOct 5, 2022

--

Creating a modular powerup system can seem daunting, but it relatively simple. We can use if statements to check for which powerup we collected.

We would need to assign each powerup a powerup ID and use the ID to check for collision.

Player Script Modifying Logic for Multiple Powerups

A better way to do this, would be to put our powerup references in an array.

To create an array, we use GameObject []. The square brackets denote the fact that it is an array. An array is a list with a set number of slots called elements. This array will go in our Spawn manager to control the spawning of the powerups.

Spawn Manager Create an Array Called powerups

We can see and manipulate the array in our inspector because of the serialize field attribute. The array we created is empty. We can add an integer value between the square brackets to set the number of elements. We can also add elements to the array as shown below. Add the prefabs for our powerups into the empty spaces.

Spawn Manager Inspector View, Powerup Array

In our Powerup Script, we need to create a powerup ID variable with a serialized field.

Powerup Script Variables

From the inspector, we can assign our powerups their powerup ID.

Powerup ID

Before, we were using individual coroutines to spawn our powerups. Now that our array is set up and the references are assigned, we can use one coroutine in the spawn manager to control the spawn of all of our powerups.

Powerup Spawn Coroutine In Spawn Manager

Create an integer variable called randomPowerUp. We’ll assign an integer value, randomly, between 0 and 2, every time the variable is instantiated. We will use this variable when calling our array to randomly select an element to spawn.

--

--

No responses yet