- Home /
Using Enum across several files
Let say that I have the following enum coded in a file.
public class EnumList : ScriptableObject {
public enum E_COLORS { RAINBOW, RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET, LENGTH };
public enum E_NUMBERS { INF, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, LENGTH };
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
How can I gain access to this from a different script file?
Answer by bradmarxmoosepi · Jul 02, 2012 at 05:28 PM
You can just write:
EnumList.E_COLORS
Or if you wanted a class that has one of those colours:
public class myNewClass : MonoBehaviour
{
private EnumList.E_COLORS _color = EnumList.E_COLORS.RED;
}
Hope this helps. I have done it a number of times myself.
Answer by Kryptos · Jul 02, 2012 at 07:26 PM
You don't need Scriptable Object to create enums. Just move the enums outside of any class in any script file you like. And they will be available in all scripts.
Don't forget to make them public
though.
Have you tried this? I get an error:
error CS0246: The type or namespace name `$$anonymous$$yEnum' could not be found. Are you missing an assembly reference?
I suspect Unity can only find types that have the same name as their containing file.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073601im_/https://answers.unity.com/themes/thub/images/avi.jpg)