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

How to Find an Easy First Issue in an Open-Source GitHub Project

My Understanding of the JWT gem -Rails Authorization

One Platform. Full-stack. Full-Lifecycle.

Basic guide to creating resources in AWS with Terraform

AWS VPC Sharing

HK share API market data

First time making an app

On August 21, 2020, at 8:00 pm, CRYPTOZONE.ID

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

Clear Shot Virtual Camera in Unity 2020

Working with Screen Space Reflections in Unity’s HDRP

Making a Treasure Chest System Using Timeline in Unity

Loading Scenes In Unity