- Home /
Manually creating Editor sometimes leads to NullReferenceException in SerializedObject constructor
This is one of those cases where it's hard to create a reproducible project, so I'd like to ask for general advice on the topic:
At Editor start,
[InitializeOnLoad]
public class EditorStartup
I register a callback when scenes get opened
EditorSceneManager.sceneOpened -= SceneOpenedHandler;
Within the callback, I search for a special component in my scene
if (EditorSceneManager.GetActiveScene() == scene)
{
scene.GetRootGameObjects(rootObjects);
foreach (GameObject go in rootObjects)
{
SceneController controller = go.GetComponentInChildren<SceneController>();
if (controller != null)
{
var editor = (SceneControllerEditor)Editor.CreateEditor(controller, typeof(SceneControllerEditor));
editor.OpenSubScenes();
break;
}
}
}
Because I have some additional code (sub-scene loading) in the editor of said component, I want to create my own editor instance from within the static callback. I might be able to put the code directly into the controller component, but I don't need it at runtime, so I believe it to be better placed in the editor side.
This all actually functions correctly. It finds my component, loads sub-scenes and so on. However, I have a small problem:
NullReferenceException: (null) UnityEditor.SerializedObject..ctor (UnityEngine.Object[] objs, UnityEngine.Object context) (at C:/buildslave/unity/build/Editor/Mono/SerializedObject.cs:26) UnityEditor.Editor.GetSerializedObjectInternal () (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorBindings.gen.cs:193) UnityEditor.Editor.get_serializedObject () (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorBindings.gen.cs:185) CrazyGirlEditor.SceneControllerEditor.OnEnable () (at Assets/Scenes/Scripts/Editor/SceneControllerEditor.cs:27)
The offending line of code looks like this
private void OnEnable()
{
subScenesProp = serializedObject.FindProperty("subScenes");
Debug.Log(GetInstanceID());
}
and the exception is coming from within the SO-constructor. I've logged the instance ID and noticed, that I have several instances of my editor in memory.
First, I must be creating some sort of leak by creating the editor manually, but I haven't found a way to obtain any existing instance, unless I try to keep track of them by myself (which I shouldn't because I don't know what Unity does with them).
Secondly, I thought that I might be calling my methods on the wrong (maybe already destroyed) instance from the last scene, but apparently I do find the correct one, because the functionality is completely correct. I can only assume, that I'm tracking the right component and editor, but at the same time, I leave behind old copies, which Unity still has referenced and calls OnEnable, but somehow weren't destroyed correctly.
Any ideas on what might be going wrong and how I could solve the issue? If possible, I'd like to keep my code in the editor class and access it from the static callback.