That was a lot of new syntax and information, but it helps to see it in action. Let's create a simple switch statement for different actions a character could take:
- Create a new string variable (member or local), named characterAction, and set it to Attack.
- Declare a switch statement and use characterAction as the match expression.
- Create two case statements for Heal and Attack with different debug logs. Don't forget to include the break keyword at the end of each.
- Add a default case with a debug log and break.
- Save the file and click on Play in Unity:
Since characterAction is set to Attack, the switch statement executes the second case and prints out its debug log. Change characterAction to either Heal or an undefined action to see the first and default cases in action:
There are going to be times where you need several, but not all, switch cases to perform the same action. These are called fall-through cases...