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.
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;
}
}
}