Creating Modular Powerup Systems

Now that we have added in a triple shot powerup let’s add in some more. Currently, the system we have made already is only really fit for 1, But with a little tweaking, we can make it fit in some more.

First, we will want to add an ID system to the Power-ups, this will make it so the game knows the difference between the different power-ups and it’s really easy to implement.

On our Power-up Script, make a variable of type int and add [SerializeField] so we can see it in the inspector. Now we can set the Triple Shot prefab to have an ID of 0, the Speedup an ID of 1, later we can add in a Shield and give it an ID of 2.

ID 2 = Shields, ID 1 = Speedup, ID 0 = TripleShot

ID system is done. Now we just need to check the ID of the Power-up we picked up by the player and add logic for what happens when we do. We can add in an if statement to check the ID before adding it to the player.

Since we haven’t made the methods yet we can just leave a comment for when they are added.

At this point, we can check if the triple shot still works and if it does we have a working system.

All that is left, is to be able to spawn the new Power-ups in.

Change over to the Spawn Manager’s Script and add in somewhere to store all the Power-ups, An Array will be perfect for this, by adding [] at the end after GameObject, this will turn it into an Array of GameObjects.

Back to the inspector. We can now add in all the Power-ups. Set the size to 3 and drag in the PreFabs for each Power-up, making sure that the element number matches up with the ID for each Power-up.

Now to pick a random ID to spawn in. Back to the Spawn Manager Script in the method for spawning the Power-ups, All we need here is to pick a random ID and spawn that ID from the list. Which would look a little something like this.

_powerID is a Variable type Int at the top, just under the class.

Here I'm using _powerUpContainer.Length, which returns how many elements are in the list, so we don’t have to come back in and change the value each time we add a new power-up. Then in the next line, we are spawning the random value we get from _powerID which at the moment is between 0 and 2, even though the length returns 3. This is because Ints in Random.Range are exclusive, meaning anything up to this number but not including.

And with that, we have a new Power-up System that can be added onto with little to no effort.

Game Developer in the making~

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