Switching it Up — Switch Statements in Unity

Photo by Markus Spiske on Unsplash

Since we plan on having more than one power-up, it’s a good time to explore the use of a switch statement instead of our If/Else statements.

What is a Switch Statement and how does it differ from an If/Else?

The main difference is that an if/else can be used for making decisions based the parameters that it’s evaluating and a switch checks against a single variable per case (with the ability of having many cases). So clearly they have their uses in the right circumstances.

How to use a Switch Statement

For our Space Shooter game we swap out our if statement to check against which Powerup the player object has collided with. We call the switch statement and point it to out _powerupID variable ease case corresponds to one of the powerups and we call to the appropriate method to invoke it from the player script. at the end of each case we call a break; and finish our switch with default case as good code hygiene. That’s all there is to it. Now our powerups are starting to take shape. We’ll program the behaviour of our shields next.




Unity Developer — Technology Leader — Making Unity Game Development Look Easy

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Result Notification №1012(MTTM-2022) Result for 3rd semester MTTM Programme Batch-2019

Funnel Rocket: A Cloud Native Query Engine

Introducing Multilingual Translation

Introducing Multilingual Translation

Is “multicloud” more than a new name for hybrid cloud?

How Continuous Testing Ensures a Seamless Migration From Oracle EBS to Oracle Cloud

Good Resources for creating a programming language

Image with the word “Programming” on it

Laravel orderBy, groupBy and limit Example

Build and Deploy Decision rules using Red Hat Decision Manager

Once RHDM is started with these parameters, login to the Business Central. After login the screen should be seen below

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Games Woods

Games Woods

Unity Developer — Technology Leader — Making Unity Game Development Look Easy

More from Medium

How to Solve Equations in Algebra Quickly!

Ask me Anything with Goldfingr Director of Operations Yarden Tamari

Hello everybody ,I hope you are all doing great !! So todays topic is ….

Why I’ll miss living in Victoria B.C.