- Home /
Single enum class
My question is would it be best too make a static class that contains all my enums for my project or have them in a script that has too do with the enum.
Answer by fendorio · Apr 20, 2014 at 04:16 PM
Just creating a .cs file will suffice - just as you'd create a class, it needs not be attached to any gameObject or inherit from MonoBehavior. So long as they're set to public you'll be able to access them.
// Your .cs file
public enum NumberOne
{
hello,
this, //Invalid syntax xD
is, //Invalid syntax xD
accessible,
anywhere
}
// End of .cs file
They need to be enclosed in a static class :)
As for whether or not you should store them all in one file, that's a subjective question really.
My opinion is no they shouldn't. Each Enum should be in it's own .cs file residing in an appropriate folder - say the folder that contains the class that uses it?
Again though purely subjective there is no right or wrong - apart from storing them in a static class :P
ah ok so public will work fine for just a script that holds all my enum's in my game ty. yeah I know what your saying I might use just a single from now on since it could be easier ins$$anonymous$$d of having in different classes.
Enums don't need to be nested in a class at all. You can declare them outside a class as well. Enums are types like a class or a struct. They are often nested in the class they belong to, to keep the global namespace clean. Commonly you would declare them in your own namespace to avoid name-collissions with other types. For example the $$anonymous$$eyCode enum is declared inside the UnityEngine namespace, but outside any class.
But is the $$anonymous$$eyCode enum declared in a file called $$anonymous$$eyCode.cs?
As far as I can tell, Unity wouldn't be able to find it otherwise.
Not necessarily. We don't have the source code of the UntiyEngine.dll. We can only decompile the dll. Commonly every type defined in an assembly is decompiled into a seperate class with a matching filename. However you can have a file called "Test" which actually contains a class with the name Test and you can still declare the enum in the same file and it can be accessed in your whole project:
// Test.cs
using UnityEngine;
public enum $$anonymous$$yEnum
{
One, Two, Three
}
public class Test : $$anonymous$$onoBehaviour
{
public $$anonymous$$yEnum fooBar;
}
Unity's $$anonymous$$eyCode enum does only exist in compiled form to us. $$anonymous$$ost Unity engine types are defined in the UnityEngine.dll
This is the correct answer, however I think you meant to say:
They don't need to be enclosed in a static class :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613143146im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to assign Enums to button? 0 Answers
Accessing Enumeration from another script issue 1 Answer
how to get enum value by looking for their hashcode? 1 Answer