C# Refreasher: Enums!

Objective: Understand what Enums are, and how to implement!

What is an Enum? Enums allow you to create readable selections that are based off of integer values. These can be used in an English format when writing code. Enums are great, particularly when used to select things such as difficulty settings, or when developing Enemy AI.

In the former, we would set up a different options for difficulty. I’m going to use the Doom difficulty settings for this example. Traditionally we would use integers variables like this.

To declare an Enum we need to use the keyword Enum.

What we can do now is inside curly brackets we can list the difficulty options, each ending with a comma except for the final one.

What this is declaring is “ImTooYoungToDie” = 0 and “UltraViolence” = 3

This is the default settings, you can always manually set the values yourself.

If we were to look at the inspector now we would only see the public integers we’ve created.

We need a variable to hold the current selections. A variable of the Enum type. For example, we just created LevelSelector Enums.

Now in the inspector we have a dropdown menu where we can select out difficulties. We could then connect these with out UI or however you want to make something easy and readable for designers.

From there we would want to work with a switch statement so we know which difficulty is selected. For the sake of this example I added a Debug.Log to print out which selection is active.

Don’t be afraid to experiment with your Enums, and I’ll see you in the next tutorial!

--

--

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