Enum Flags In Unity and C#

Enum Flags are a C# feature that allow for multiple enum values to be applied to a single enum variable.

Enum Flags:

Enum Flags allow for multiple enum values to be applied to a single enum variable. This feature is a part of C#’s System namespace.

First, add the Flags attribute to your enum list:

This will allow multiple Enum constants to be selected, as well as adding an “Everything” option which selects all states:

However, just this is not quite enough. As it currently is, choosing the states you want will not necessarily select them in the way you intended.

Using the Flags attribute indicates that the Enum can be treated as something called a Bit Mask.

A bit mask is, essentially, an integer value in which several binary properties (yes/no) are independently stored in its bit. -Source

That is because the Enum uses integer index IDs behind the scenes to identify the different states. These IDs are also not just 0,1,2,etc. They are

By manually assigning the IDs in Power-Of-Two form, it allows multiple options to be chosen at once.

Once these are manually defined, the Enum can be compiled and interpreted in the desired way, resulting in:

When done correctly, the Nothing and Everything options are automatically created

--

--

--

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.

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

Recommended from Medium

Beyond Design Thinking, Towards Programming Thinking

Shipkit — getting started

All The Resources You Need To Become A Frontend Engineer

Governance Proposal: ADDY Buybacks

Top 12 PHP IDEs For PHP Programmers in 2021

PHP IDEs For PHP Programmers in 2021

Understanding the Concept called — “DEVOPS” part 1

C Programming Basics.

C Programming Basic

I’m new to this site, not really sure what it’s all about…kind of looks like a modernistic “tumblr”…

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
Vincent Taylor

Vincent Taylor

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.

More from Medium

Creating MineField In Unity (Part 1)

Did you just hit me! — Hitbox attack system in Unity2D

Zoo Cards as Scriptable Objects

Basics of Optimization in Unity