Creating a Modular Powerup System
Having one powerup for a player is great - but giving him more options and more variety… is even better! Therefore I will have to make my powerup system a bit more modular so that I can reuse it for multiple different ones!
For a modular powerup system, we need to create an ‘Id’ system to identify each powerup easily. We need an ID system so that each power-up can be identified. There are different ways how this can be achieved, either by creating a variable of type int, using an enum - or in this case, by using a list.
So, in the ‘Powerup’ script, I will have to identify each powerup with an id:
And in the SpawnManager, I will be adding a list of powerups:
And populating that list with three powerups:
Now, all we need to do is to update the ‘OnTriggerEnter2D’ method in the ‘Powerup’ script to differentiate between the three. I will be using a ‘Switch’ statement here; more on that in the following article!
And also updating the ‘SpawnManager’ script to spawn all three of these powerups:
As we can see, this code is now modular. When we add different powerups in the future, all we have to do is create another ‘case’ in the ‘Powerup’ script and adjust the ‘Random.Range’ in the ‘SpawnManager’ script.
But that is it for now, thank you for reading and feel free to follow me for more articles - and as always, good luck and see you next time!