- Home /
Question by
Rukas90 · Mar 22, 2019 at 01:50 AM ·
c#objectdebuggingpropertydrawersystem.type
Get and Set System.Object value using SerializedProperties
Hey everyone,
I have this one issue, 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) + 26;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
GUI.Box(position, GUIContent.none);
var typeEnum = property.FindPropertyRelative("type");
position.y += 5;
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", EditorStyles.centeredGreyMiniLabel);
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;
}
}
}
error.png
(5.9 kB)
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Repeating the loop 0 Answers
Input.GetMouseButton NEVER returns false 1 Answer
How to use tag for all players in the Instantiate. 0 Answers