1

I want to be able to specify the type with an enum and assign the value to the object variable, but I keep getting 'NullReferenceException ..' error message.

enter image description here

I have this simple class:

[System.Serializable()]
public class ObjectValue
{
    public enum ValueType { Null, Integar, Float, String, Boolean }
    [SerializeField, HideInInspector] private ValueType type = ValueType.Null;

    public object value = null;
}

And by the help of a property drawer script I want to be able to view this class on the Inspector and edit the value depending on the chosen type:

using UnityEditor;
using UnityEngine;

[CustomPropertyDrawer(typeof(ObjectValue))]
public class ObjectValue_Drawer : PropertyDrawer
{
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        return base.GetPropertyHeight(property, label) + 17;
    }
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        GUI.Box(position, GUIContent.none);
        var typeEnum = property.FindPropertyRelative("type");

        position.height = 17;
        EditorGUI.PropertyField(position, typeEnum);
        position.y += 17;

        var value = property.FindPropertyRelative("value");
        switch (typeEnum.enumValueIndex)
        {
            case (int)ObjectValue.ValueType.Null:
                GUI.Label(position, "Null Value Type");
                break;
            case (int)ObjectValue.ValueType.Integar:
                value.intValue = EditorGUI.IntField(position, "Value", value.intValue);
                break;
            case (int)ObjectValue.ValueType.Float:
                value.floatValue = EditorGUI.FloatField(position, "Value", value.floatValue);
                break;
            case (int)ObjectValue.ValueType.String:
                value.stringValue = EditorGUI.TextField(position, "Value", value.stringValue);
                break;
            case (int)ObjectValue.ValueType.Boolean:
                value.boolValue = EditorGUI.Toggle(position, "Value", value.boolValue);
                break;
        }
    }
}
UnknownUser
  • 327
  • 2
  • 14
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – BugFinder Mar 22 '19 at 07:13
  • @BugFinder This is *not* a duplicate.of a generic question about a null reference exception. It's a perfectly valid question in the context of Unity inspector UI, even though the context could've been clarified better in the question itself. – Lumos Dec 07 '19 at 21:25

1 Answers1

0

I'm afraid that, as of Unity 2019.2 and the time of writing of this answer, this is impossible directly with objects or other types that don't register as serialisedProperties, but can still be done with a work-around.

Said work-around consists of (ab)using the ISerializationCallbackReceiver interface and thus circumvent this issue by using a malleable intermediate representation for the editor. This actually works!

On a partial tangent: if using something that is a serialised property, you can use hacks to access the value directly through the use of reflection. This is helpful in our case, should you wish to manually call the custom class's OnBeforeSerialize method inside the Editor, for instance.

Lumos
  • 358
  • 3
  • 10