- Home /
Accessing enum from editor script returns null
/// In script:
enum city { London, New York, Toronto }
/// In Editor script:
function OnInspectorGUI
{
Debug.Log( target.city );
}
It returns null, when I'm expecting it to return the enum.
Answer by Kryptos · Nov 23, 2012 at 08:46 AM
An enum is like a type. You just declared it but your script is not using it.
What you need to do:
/// In script
enum City // use uppercase
{
London,
NewYork, // you cannot use whitespace
Toronto,
}
City myCityVariable = City.Toronto;
/// In editor script
function OnInspectorGUI
{
Debug.Log( target.myCityVariable );
}
But if you want to log this enum as a type, you need to access it as a class-variable:
Debug.Log( YourScript.City.ToString() );
where YourScript
is the name of your script (in fact the name of the class defined in your script).
I have no problem getting a variable using the enum, but how can I get a list of the items in that enum?
Basically what I want to do is display a toolbar that will let me choose amongst the enum values, but the toolbar interface element requires a string array of the items, so I need to get all the enum values to be able to convert them into a string.
System.Enum.GetNames(typeof(YourScript.City))
Cool. I got it to work. Rather than using your example of referencing the enum, I needed to reference the variable instance of the enum, so…
System.Enum.GetNames(typeof(YourScript.myCityVariable))
Thanks Nicolas! :)
Your answer
Follow this Question
Related Questions
how to fix a null reference exception when a findgameobjectwithtag does not exist 1 Answer
NullReferenceException: Object reference not set to an instance of an object... 0 Answers
Exposing Enums in Custom Inspector 1 Answer
Best practice handling deleted object refences during SceneManager.LoadScene 0 Answers
this error 10's of times in editor...? "NullReferenceException:..." 0 Answers