Skip to content

Creating a Popup/Dropdown in the Editor: EnumPopup vs. Popup and EnumValueIndex vs IntValue – Unity

I’ve seen a bit of confusion around Unity editor controls for dropdowns (or popups as Unity calls them) especially when used with a SerializedProperty. Should I use EnumPopup or Popup? enumValueIndex or intValue? Why am I getting “enum index is out of range” or Cannot convert type `System.Enum’ to `int’?

I had a rather mystifying problem when the value set in my Keycode Popup drop down did not match what showed up as the actual enum value during debugging, so I decided to dig deep to fully understand and clarify the issue.

enumValueIndex vs. intValue

intValue is what you would expect. It’s the literal int value of the enum. enumValueIndex is the index that would be used if all the enums would be condensed into a minimally sized array. If you’re not setting the int values of your enums manually, they’ll be functionally the same. However, if you set specific int values to some of your enums, then you’ll want to use the value appropriate to your form (which is discussed in the next section).

For example:

public enum EnumWithGaps
    Zero = 0,
    Eleven = 11,
    Random = 236,
    Size = 3

If the enum was set to Zero, intValue and enumValueIndex would be the same: 0. However, if the value was Eleven, the intValue would be 11 and the enumValueIndex would be 1 as the condensed array would be [Zero, Eleven, Random, Size]. These properties update each other so setting the intValue will automatically update the enumValueIndex and vice versa.

EnumPopup vs. Popup

If you are not using a SerializedProperty, you can use EditorGUILayout.EnumPopup to keep things simple. Unity will automatically grab the options.

KeyCode newKeyCode = (KeyCode)EditorGUILayout.EnumPopup(oldKeyCode);

If you are working with a SerializedProperty things are slightly more complicated. EnumPopup will require a bit of casting, but works well. You’ll want to use intValue here as EnumPopup is returning your enum. (Casting to the enum first is important as you won’t be able to cast to int directly as discussed in the next section.)

property.intValue = (int)(KeyCode)EditorGUILayout.EnumPopup((KeyCode)property.intValue);

If you want more flexibility (and to avoid having to cast to your enum), you can use a normal Popup with the values SerializedProperty provides. Use enumDisplayNames for the displayedOptions and pass in the enumValueIndex as the selectedIndex. This is important. You want to provide the index into enumDisplayNames and you’ll receive the index in that array back out.

property.enumValueIndex = EditorGUILayout.Popup(property.enumValueIndex, property.enumDisplayNames);

Cannot convert type `System.Enum’ to `int’

When using EnumPopUp, you’ll want to cast to an int to set the SerializedProperty. However, you won’t be able to convert directly from the System.Enum type returned. Casting first to your selected enum and then to int as shown above is the simplest solution. You could use System.Convert.ToInt32() on the result, but you still won’t be able avoid casting your property.intValue to your enum to pass it into EnumPopup.

enum index is out of range

You may encounter this if you use enumValueIndex with EnumPopup or intValue with Popup. Check out the above to resolve your issue. You may also encounter this if you don’t set a default value for an enum and your enum doesn’t have an enum value for zero.


That’s all! Hopefully that settles any confusion on how to set up drop downs for enums in the Unity editor and saves you some time!

Published inCode ExamplesDevelopment Tips

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *