In the following code
(copied from this question: How do I support Undo without using properties? - Unity Answers)
myClass is succesfully created, and so is not null. But after the line
myCustomClass = serializedObject.FindProperty("myClass");
I can see in the debugger that myCustomClass IS null, which then causes a NullReferenceException when I try to access it later in:
EditorGUILayout.PropertyField(myCustomClass, true);
I’ve stepped through this in the debugger, but I can’t understand why a non-null is returning a null.
Should custom classes be able to be serialized in this way?
[CustomEditor(typeof(NewBehaviourScript))]
public class MyEditor : Editor {
SerializedProperty myInt;
SerializedProperty myCustomClass;
void OnEnable(){
myInt = serializedObject.FindProperty("myInt");
myCustomClass = serializedObject.FindProperty("myClass");
}
public override void OnInspectorGUI(){
serializedObject.Update(); //don't forget this
EditorGUILayout.PropertyField(myInt);
EditorGUILayout.PropertyField(myCustomClass, true); //shows children too
serializedObject.ApplyModifiedProperties(); //or this
}
}
public class NewBehaviourScript : MonoBehaviour {
public int myInt;
public MyClass myClass;
void Reset(){
myClass = new MyClass();
}
}
public class MyClass{
public string aString;
}