Switch Statements (Game Dev Day 20)

Jack Leavey
Sep 5, 2021

As briefly shown in my previous article, switch statements can be used in Unity for scripts that need to have multiple possible outcomes.

This switch statement controls which powerup will spawn.

Instead of having 3 unique scripts (one of each of our powerups) we can just reference the appropriate coroutine on our Player script based on the value assigned here at the switch command. If we made another power up that made the player shoot 10 lasers, we could just add another option to the switch command (and create the appropriate coroutine in the Player script.

If you are making a system that needs multiple outcomes for the same action, you can also use a switch statement and use a random integer (or integer range) for the applicable coroutines.

--

--

Jack Leavey

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!