Switch Statements To The Rescue!

Joseph Bonney
2 min readJan 12, 2022

--

Switch statements Make it easy to add features to a game. It is a series of events that you can call and switch to depending on the case. In my case, I created a modular powerup system as seen in an article I wrote a few days ago, ( Here: https://medium.com/@jebonneygames/creating-a-modular-powerup-system-in-unity-c31e06fb8c17 ), After adding more powerups to my game it was pretty easy to switch between and add behaviors and more powerups using the same script by using a powerup ID system, then creating methods to call during each switch case.

You can use switch cases for any number of different things. They work very similar to if and else if statements, however they are much more streamlined as if and else if statements can become very cumbersome in most cases. If there is a constant that you can base it off of, to alleviate the burden and chaos of if and else ifs, you can just use switch statements. Like if you have a stat system and you want different behaviors to happen if your stat reaches different points. When you want it to change, just make a case for it and call the change in the case. Like in my case of the powerups. By assigning an ID, I can switch cases based on those ID’s. So it was easy to implement different powerups with different behaviors in the same script. Switch statements save the clutter and chaos of if and else if statements. Switch statements to the rescue! Thank you for reading =).

--

--