Unity Basics: Switch Statements to the Rescue

Code can always be optimized to be more efficient. Refactoring code is normal and even very necessary. As a beginner, the most important thing is to write code that solves a problem, and the next step is to optimize the code to make it readable and modular.

Take a look at this example of the powerup system in Space Shooter:

The -if- and -else if- statement does the job here, but with more elements, it will start to be quite difficult to read and maintain. If we use a switch statement here, our code will be much nicer ;)

It’s important to make constant code refactoring a habit, go back to your code and refactor it every time you develop, learn new things or just when looking at your code again you have an idea how to improve it.

--

--

--

Hi, I’m Damian, a Unity Software Engineer who loves building education and simulation games.

Recommended from Medium

Getting started with Azure Bot Framework: Introduction

How to become a Web Developer? (Part 1)

Copy Storage blob

Microsoft Azure Blob Storage

May 20, 2020

Google cloud associate engineer exam prep!

Image result for google cloud exam associate engineer

How enterprises can overcome barriers to DevOps — WalkingTree Technologies

Creating collectables | Unity

Launching DHC Liquidity Mining Program

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
Damian Dąbrowski

Damian Dąbrowski

Hi, I’m Damian, a Unity Software Engineer who loves building education and simulation games.

More from Medium

Cinematography in Unity — Transparent Windows

Camera Shake in Unity

Adding a camera shake effect

Bug Fix on Model Flip