My very first project: Space Shooter

How to create a modular system for powerups

Also adding a speed boost powerup

How to make my enemies lives more miserable? By giving my player a speed boost of course! Since this is done basically the same way as the triple shot powerup, I won’t go into the details here. But its the same principle, a coroutine which ticks the time off the speed boost and a public method which alters the speed of the player. Same applies for the animation and prefab parts, pick the right folder from the Filebase assets and save the powerup as a prefab.

The “harder” part comes next. I could add similar lines of code to my SpawnManager for the speed boost as I did for the triple shot, and give it also some random time interval for when one would be instantiated. This could be a viable option, since I will not have that many powerups. But what if I have some sort of burst of ideas for all sorts of powerups? How many lines of code would I have to write then? What about the timer? Should I use the same one for all? Make a different one? What happens when I use ten different timers for ten different powerups? What would be the logic which powerup will spawn and when?

All these are good questions. They also show how the problem comes bigger and bigger with each additional powerup. Then there’s the issue of maintainability. With ten powerups there are ten instantiations, one for each, possibly ten timers, logic to handle the spawning and who knows what else to just make it all work. The bare minimum is 11 lines of code, with no logic whatsoever. This would mean that all of the powerups would spawn at the same random interval, which would lead to a situation where the all of the powerups would just rain down endlessly, boosting the player all the time. So there would have to be some logic, which would make the maintainability of the code harder, especially if I were to introduce an eleventh powerup.

With this in mind, I should probably think of another way of instantiating my powerups. One which could take into account the possible future updates to my roster of powerups. This is where the modularity comes into play. I can achieve it by adding all the powerups into an array of GameObjects, instead of saving them into individual GameObjects.

To do this, all I have to do is to make a private GameObject called powerups and add square brackets after the GameObject, as seen in the image. Once I use the [SerializeField], I can go to Unity, change the array size to two and populate the fields with the triple shot and speed boost powerups.

This is all it takes to make my powerups modular. Well almost. Now that I have them in my GameObject array, I still have to instantiate them so that they will appear in the game. Because I have two powerups, I will use a random selector, which selects the next powerup. I will also change the interval to anywhere between three to five seconds so that the powerups will spawn more frequently, since I have no way of knowing which powerup will spawn and when. That is, if my player is unlucky this method could spawn only one type of powerup for the whole duration of the game.

The best part is, that if I do come up with new powerups, all I have to do is to increase the size of the array and add the new powerups to there. Without forgetting to increase the random selector’s range of course.

Unity game developer in the making.