C# Switch Statements in Unity

What are switch statements and when to use them

Tristan Engel
May 15, 2021

Switch statements work like a series of if else statements. When checking the same variable for multiple values it is advised to use a switch statement.

Same functions first one with if statements, second one with an switch statement.

If else statement series
Switch statement

The syntax is a bit different for switch statements. Instead of using {} for each case you end the case with break;
Break means it will stop going further into the switch statement and exit from that part of code. The default case is like the else for if statements and is advised to put in to check for errors.
The biggest advantage of using a switch statement for these cases is to get a better overview and manageability when checking for many cases.

--

--

Tristan Engel

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.