Editor.CreateEditor() and UnityEvent properties

I’m getting a weird issue with custom Editors and UnityEvents.

Here’s the setup

  1. ComponentA has a UnityEvent property.
  2. ComponentB has a public property “componentA” which references an instance of ComponentA.
  3. ComponentB has a custom Editor defined. In ComponentBEditor.OnInspectorGUI(), I’m using Editor.CreateEditor(componentA) to inspect the properties of componentA, by calling DrawDefaultInspector() on the created Editor.

The created Editor works perfectly for any type of property I put in ComponentA, but as soon as I add a UnityEvent property I get the following console error. Am I missing something or is this a bug?

NullReferenceException: SerializedObject of SerializedProperty has been Disposed.
UnityEditorInternal.ReorderableList.get_count ()
UnityEditorInternal.ReorderableList.GetListElementHeight ()
UnityEditorInternal.ReorderableList.GetHeight ()
UnityEditorInternal.UnityEventDrawer.GetPropertyHeight (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label)
UnityEditor.PropertyDrawer.GetPropertyHeightSafe (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label)
UnityEditor.PropertyHandler.GetHeight (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, Boolean includeChildren)
UnityEditor.PropertyHandler.OnGUILayout (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, Boolean includeChildren, UnityEngine.GUILayoutOption[ ] options)
UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, Boolean includeChildren, UnityEngine.GUILayoutOption[ ] options)
UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, Boolean includeChildren, UnityEngine.GUILayoutOption[ ] options)
UnityEditor.Editor.DoDrawDefaultInspector (UnityEditor.SerializedObject obj)
UnityEditor.Editor.DoDrawDefaultInspector ()
UnityEditor.Editor.DrawDefaultInspector ()

I was having the exactly same problem. I was calling the Dispose() method of the SerializedObject in the OnDisable() method of my custom editor. I’ve commented out this method and everything worked as expected…

I hope this is in time to help you,