PropertyField renders enums inside SerializeReference fields as integers

When you render a field that’s serialised via SerializeReference with EditorGUI.PropertyField, all the enums inside that object show up as integers. Minimal example to reproduce this:

using System;
using UnityEngine;

public enum MyEnum
{
    OneValue,
    AnotherValue
}

[Serializable]
public class MySerializableClass
{
    public MyEnum EnumField = MyEnum.AnotherValue;
}

public class TestScript : MonoBehaviour
{
    [SerializeField] private MySerializableClass serializedField = new MySerializableClass();
    [SerializeReference] private MySerializableClass serializedReference = new MySerializableClass();
}

And then create a simple property drawer:

using UnityEditor;
using UnityEngine;


[CustomPropertyDrawer(typeof(MySerializableClass))]
public class MySerializableClassDrawer : PropertyDrawer
{
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        return EditorGUI.GetPropertyHeight(property, true);
    }

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        Debug.Log(property.propertyPath);
        EditorGUI.BeginProperty(position, label, property);

        EditorGUI.PropertyField(
            position,
            property,
            true);

        EditorGUI.EndProperty();
    }
}

See attached screenshot for the result.

I’m going to report this as a bug as well (because I think it is one?). But in the meantime, does anyone have a workaround? Manually rendering the property field isn’t really an option in my actual project since the enums show up at various depths in various objects.

I tried writing a custom property drawer just for that enum type, but that actually fails at property.enumValueIndex, suggesting to me that enums don’t actually get serialised as enums inside serialised references?

EDIT: Retrieving the enumValueIndex via property.intValue does work for both serialised fields and serialised references. But as a workaround, it still means I’ll need to implement one property drawer for each enum type I’ve got (probably quite doable with a generic base class, but still a pain).

EDIT2: Corresponding bug report: http://fogbugz.unity3d.com/default.asp?1197948

EDIT3; Turns out this bug is already known and currently marked as WONTFIX: Unity Issue Tracker - Enum fields are shown as int fields in the Inspector window when tagging them with SerializeReference attribute. I’m still interested in workarounds that don’t involve having a custom property drawer for every enum type, and avoiding ever using property.enumValueIndex.

5164757--512294--enum-inside-serialisereference.png

1 Like

Looks like this actually did get fixed in 2019.3.0.11b.

Weird. I’m getting this in 2021.3.8 LTS using UIElements.PropertyField, though it only happens sometimes. I’ll update if I figure out why.

I have a SerializedObject I create. Instead of calling serializedObject.Update(), setting it to a new serialized object every time averts the issue for some reason…

Make sure your enum is set as “Serializable” (System.Serializable) or else Unity might have some issues.

Unity - Scripting API: SerializedObject.Update (unity3d.com)

Thanks for the help. I do have my enums set as Serializable, though I am using some other enums I am unable to change. The issue occurs in both cases. About SerializedObject.Update(), I’m aware that it updates the serialized object to the data of the inspected object. What I’m not sure about is why that would cause enum fields to display as integers…