Switch Statements to the rescue!

Dennisse Pagán Dávila
Level Up Coding
Published in
3 min readApr 25, 2021

--

In the previous article, we went over the process of creating a Modular Power-Up System where we used if-else statements to detect the power-ups and then execute their effects accordingly. Even though using if-else statements to accomplish this logic results in a functional code, it is best practice to use a switch statement if you have multiple if-else. It makes your code more concise and clearer to understand. Furthermore, in the event that you have even more power-ups, the implementation will be more optimized, and easier to achieve this way.

How and when to use them

A good rule of thumb to remember is to always use Switch Statements if your code requires more than two if-else statements. Switch Statements allows us to check for a variety of different cases, then execute the necessary actions all in one neat block of code.

  • condition: An expression that can be an integer(int), an enumerate, or character type expression.
  • case: Each is followed by a value that is being compared to the expression. Then, rather than using a semicolon(;), a colon(:) is used to determine that the next line of code is an executable action.
  • break: Denotes that you are done and no further actions are needed in that particular case.

You can have any number of cases in a Switch Statement. A case is executed when the value in the condition expression is equal to the compared value in a given case.

How if-else statements can be converted into Switch Statements

Let’s take a look a the Modular Power-Up System code from the previous article:

The code makes use of multiple if-else statements to activate the different power-ups, this can be cleaned up and, optimized by converting it to a switch statement.

  1. You still need to create a global int variable to keep track of the current power-up ID. This will be the expression in the Switch Statement.

2. I used the inspector to set the respective values to my power-up objects, this is how the code will differentiate them later.

3. Previously, we had a set of int variables assigned to each power-up, this is not necessary for the Switch Statement. With the expression alone, you can start the comparison directly. The cases can have any int valued assigned to them since the expression is of type int as well. I decided to go with ascending values, but these numbers could have had any order as long as they remain the same type as the expression. Your power-up behavior code should go under the case in the switch statement — it’s the equivalent of saying “in case this happens, do this”

Note: The numbers assigmend to the power-up objects in the inspector must match the ones in the code.

Note:You may notice that you get an error or the foreboding curly red line under your code when writing a Switch statement, this is because the compiler is waiting for you to add the break.

Now, you have a fully functional and optimized Modular Power-Up System! In my next article, we’ll take a look at building UI elements in Unity!

--

--

Software Engineer that specialize in Game Development. Currently looking for new opportunities. LinkedIn: https://rb.gy/xdu8nu