Skip to content

SerializedProperty for Enum with Popup: enumValueIndex vs intValue – Unity

I’ve seen a bit of confusion around Unity editor controls for enums. 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.

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].

EnumPopup vs. Popup

If you’re not using a SerializedProperty, you can use EditorGUILayout.EnumPopup to keep things simple.

If you’re working with a SerializedProperty things are slightly more complicated. EnumPopup will require a bit of casting, but works well. (Casting to the enum first is important as you won’t be able to cast to int directly.)

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. (The intValue will automatically be updated when enumValueIndex is updated.)

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. You can either cast first to your selected enum and then to int as shown above or use System.Convert.ToInt32().

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.

Summary

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 *