Editor.CreateEditor (491788)

Hi,

I get a null reference exception when calling the OnInspectorGUI on an editor created with Editor.CreateEditor.
The object passed inherit from UnityEngine.object and a custom editor is available.

How I create the editor :

	public void OnEnable()
	{
		Actor = target as Actor;
		HUDTargetData = Actor.HUDTargetData;
		HUDTargetDataEditor = Editor.CreateEditor(HUDTargetData) as HUDTargetDataInspector;
	}

And I get the error here :

	public override void OnInspectorGUI()
	{
		ActorFoldout = EditorGUILayout.Foldout(ActorFoldout, "Actor");
		if (ActorFoldout)
		{
			HUDTargetDataEditor.OnInspectorGUI(); // NullReferenceException: Object reference not set to an instance of an object

			if (GUI.changed)
			{
				EditorUtility.SetDirty(target);
			}
		}
	}

If found the solution :

	public Editor HTDI;

	public void OnEnable()
	{
		Debug.Log("OnEnbale ActorInspector");
		Actor = target as Actor;
		HUDTargetData = Actor.HUDTargetData;
	}

	public override void OnInspectorGUI()
	{
		if (HUDTargetData  !HTDI)
		{
			HTDI = Editor.CreateEditor(HUDTargetData);
			Debug.Log("create HTDI");
		}

		FoldOut = EditorGUILayout.Foldout(FoldOut, "Actor");
		if (FoldOut)
		{
			HTDI.OnInspectorGUI();
			if (GUI.changed)
			{
				EditorUtility.SetDirty(target);
			}
		}
		DrawDefaultInspector();
	}
1 Like