Firstly I am missing my inheritFrom field when using the normal Inspector (not anymore when I inherit from UnityEngine.Object, then there’s just 1 field):
[DataContract]
[System.Serializable]
public class BotPersonality : UnityEngine.Object
{
[DataMember]
[JsonProperty]
[SerializeField]
private BotPersonality _inheritFrom;
[DataMember]
[JsonProperty]
[SerializeField]
private string[] _possibleNames = {"test1", "test2"};
I want to write a custom inspector for inheriting / copying fields from another object with _inheritFrom
.
However I am getting NullReferenceExceptions
( SerializedObject.FindProperty
returns null
) for all my fields.
it says it can’t find the skill
property, but it is clearly defined and it works in the first image.:
[DataMember]
[SerializeField]
public int skill = 1;
The skill
variable is serializable.
Below is my PropertyDrawer class
[CustomPropertyDrawer(typeof(BotPersonality))]
public class BotPersonalityDrawer : PropertyDrawer
{
private readonly Type _type = typeof(BotPersonality);
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
BotPersonality botPersonality =
Utils.GetActualObjectForSerializedProperty<BotPersonality>(this.fieldInfo, property);
this.ShowPersonality(botPersonality, property);
}
private void ShowPersonality(BotPersonality bot, SerializedProperty property)
{
int prevIndentLevel = EditorGUI.indentLevel;
foreach (FieldInfo fieldType in this._type.GetFields()
.Where(x => x.IsDefined(typeof(DataMemberAttribute), false)))
{
...
var serializedProperty = property.serializedObject.FindProperty(fieldType.Name);
EditorGUILayout.PropertyField(serializedProperty);
}
}
}
The code for GetActualObjectForSerializedProperty can be found here: http://sketchyventures.com/2015/08/07/unity-tip-getting-the-actual-object-from-a-custom-property-drawer/ (I changed it so it works with lists too ), but this an object that is not null.
Help would be much appreciated. I can’t see what I’m doing wrong.
EDIT: To clarify, I’m not looking for help with the inheritance / copy an object.
EDIT2: I’ve created it as small as I can and it still gives the same NullRef Exception:
[DataContract]
[System.Serializable]
public class BotPersonalitySmall : MonoBehaviour
{
[DataMember]
[SerializeField]
public int skill = 1;
[DataMember]
[SerializeField]
private BotPersonalitySmall _inheritFrom;
[DataMember]
[Overridable]
public BotPersonality.WaypointSteeringInfo waypointSteering;
}
The class I’m trying to serialize
The PropertyDrawer:
[CustomPropertyDrawer(typeof(BotPersonalitySmall))]
public class BotPersonalitySmallDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// Using BeginProperty / EndProperty on the parent property means that
// prefab override logic works on the entire property.
EditorGUI.BeginProperty(position, label, property);
var serializedProperty = property.serializedObject.FindProperty("skill");
EditorGUILayout.PropertyField(serializedProperty);
EditorGUI.EndProperty();
}
}
It still can’t find the property skill
. I’ve changed the inheritance on BotPersonality
to UnityEngine.Object
or keep it empty, but that didn’t help.