Learn to code by making 2.5 D Space Shooter game.

Object: Understand switch statement by optimize Powerup script.

First we need to know that Switch statement work the same as if else statement, But switch statement is more clear and make code to look clean.

But switch statement use switch keyword double parentheses end with curly braces

Inside double parenthesis we pass a condition we need to check.

Switch statement check condition by using what called a case followed by the value end with colon.

Note: if we leave like this our switch will have error and wont work , because for every case you need to tell our switch here is the end of our case we use break with semi colon to tell our switch statement here is the end.

Now lets finish add other case for speed boost and shield powerup.

Now our code look more clean .

The best Practice to use switch statement is when you have more than 1 if else statement.

See you in the next article

--

--

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
Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development