Modular Power-ups

Till now we’ve just had one power-up to deal with, but in a full-fledged game, we might have ten, fifty or a hundred powerups that we may encounter through different sections of a game. Therefore it doesn’t make sense to have that many scripts pretty much doing more or less the same thing. This is where C# really shines, because we have something called Object-Oriented Programming, and we can create a modular system depending on our needs.

The Switch Statement

If you think about all the power-ups, although they differ they are still one object. But to define the different power-ups in one script and thus making it modular, we’ll be using something called a “Switch” Statement. Every Switch statement will have something called a “Case”. Every case will have an id starting at 0. In our project, we will have 3 power-ups, and therefore the first case will have an ID of 0 and the 3rd case will have an ID of 2.

The Code

In our code, we’ve defined 3 power-ups, and on the individual power-ups, we have to specify which power-up they are by assigning the value. Zero is the TripleShot Power-up, one for the Speed boost and two for the shield. With this system in place, we can add as many power-ups as we want, and we can allocate them easily from the inspector.

See how amazing the modularity of Switch Statements and Object-Oriented Programming is. We can look at one script and know exactly what’s going on without having to read through multiple scripts.

--

--

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
Dominique Dos Santos

Dominique Dos Santos

Self-taught Unity and C# Developer with a passion for games and the stories they tell.