Day 20 — Getting familiar with switch statements in Unity

Connor Fullarton
4 min readApr 4, 2021

Hey and welcome!

Alright I’ve got a fun one for you here where we’re going to be going over a pretty handy use for a switch statement in our game.

First up though we’re going to create a SpeedPowerup for our player to collect. So go through the usual steps you did for the triple shot including the animation and meet back here for some code.

Right now it’s time to implement the behaviour that happens when the player picks it up which is that we’re going to double the player’s speed. One way we could go about it is have an EngageSpeedPowerup() method with accompanying Coroutine similar to our triple shot and simply have _playerSpeed = 16f; in our code for it. Which is definitely a fine way to go about it but I want to make use of our bools that we’ve learned before.

private float _playerSpeed = 8.0f;
private int _doubleSpeed = 2;
private bool _collectedSpeed = false;
private void Movement()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(horizontalInput, verticalInput, 0); if (_collectedSpeed == false)
{
transform.Translate(direction * _playerSpeed * Time.deltaTime);
}
else if (_collectedSpeed == true)
{
transform.Translate(direction * (_playerSpeed * _doubleSpeed) * Time.deltaTime);
}
}

It’s been a hot minute since we’ve last seen this code but you should recognise it as our player movement code. The main takeaway here are the two new variables we’ve created _doubleSpeed and _collectedSpeed. Our _collectedSpeed is a bool checking to see if we have the speed powerup, if we don’t then it runs the regular movement code we know in love but if we do have the powerup then we’re going to play some edited movement code that multiplies our _playerSpeed by the _doubleSpeed variable.

Doing it his way means we don’t have to directly change our _playerSpeed. With that in place this bit of code starts looking pretty similar to our triple shot:

public void EngageSpeedPowerup()
{
_collectedSpeed = true;
StartCoroutine(SpeedPowerupTimer());
}
IEnumerator SpeedPowerupTimer()
{
yield return new WaitForSeconds(5.0f);
_collectedSpeed = false;
}

The switch statement

With that code in place now we get into the fun of switch statements. First though we need a way to identify our powerups by id, if you’ve attempted to add a reference to the EngageSpeedPowerup() method to the Powerups script you may have noticed that we’ve got no way to distinguish between the powerups when our player collides with one.

[SerializeField]
private int _powerupID;

Thankfully it’s an easy enough fix with the above code here, now it’s just a case of assigning an ID to our powerups through the inspector

I went and assigned an ID of 0 and 1 to my triple shot and speed powerups respectively. It’s important to know that in programming numbers often start from 0, not too important here but when we start getting into arrays that’s when it gets really important.

Marvellous now we can get a reference to our powerups by the ID associated with them, with what we’ve learned so far we’d be checking for them like this:

if (_powerupID == 0)
{
//Engage the triple shot
}
else if (_powerupID == 1)
{
//Engage the speeeed
}

Now that’s fine as is but I plan on adding a third powerup which would mean I need another else if statement on top of this and that’s when things start looking wild. Good thing switch statements exist:

switch(_powerupID)
{
case 0:
_player.EngageTripleShot();
break;
case 1:
_player.EngageSpeedPowerup();
break;
case 2:
Debug.Log("Shields");
break;
default:
Debug.Log("Something has gone horribly wrong");
break;
}

This neater looking bit of code is doing exactly the same as an if statement plus three else if statements.

First in the switch(_powerupID) we’re giving the variable that’s getting checked, then the case are there to show what happens if the _powerupID ends up being a certain number and initiates the code accordingly.

So in the case that _powerupID equals 0 we’ll run the code for engaging the triple shot and likewise in the case that the _powerupID equals 2 we’ll run the code for shield and so on.

It’s really handy stuff here, especially if you had a large number of things to be checking. As an example a switch statement could be used for say a character customiser. So if there were 20 different hair colours you could assign each of them a number that gets stored in something like _hairColourID and then we can use the switch statement to check all 20 cases of the 20 numbers/IDs stored in there.

--

--

Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.