EditorWindow is blank when restarting Unity Editor

Hi!

I’ve written a pretty complex EditorWindow script and had once to restart the Editor with my EditorWindow open. After restart I noticed that my EditorWindow showed up but it was completely blank, all GUI elements just disappeared. Now before I continue to write more code, first I wanted to get rid of this “bug”. I made a new simple EditorWindow from a tutorial with just one button and restarted Unity. And the window was blank aswell, the button was gone.

Here is the code:

using UnityEngine;
using UnityEditor;
using System.Collections;

[System.Serializable]
public class EWTest : EditorWindow
{
	public static EWTest window;
	
	[MenuItem("Window/Test")]
	public static void Init()
	{
		window = (EWTest)EditorWindow.GetWindow(typeof(EWTest));
		window.autoRepaintOnSceneChange = true;
	}
	
	public void OnGUI()
	{
		if(window)
		{
			GUI.Button(new Rect(10, 10, window.position.width, 30), "TEST");
		}
	}
}

I searched on the forum, on Unity Answers, in google but couldn’t find any information about this issue.
I would appreciate any help, or a link to script reference, anything…

Thanx in advance!

You need to store the Information on an Actual Object. Such as adding a script to an actual object and then accessing the fields in that object that will relate to with whatever your doing…

Thank you for your help, Eiznek, but I found the reason why my window is loosing the content. It’s the “if(window)” line. I shouldn’t test window, because it’s always null. After that I got “NullReference” error on “window.position.width”. I don’t know why I didn’t just use “position.width” instead… Now this window and my own EditorWindow start just fine after restarting Unity.

Here is the fixed OnGUI():

void OnGUI()
{
	GUI.Button(new Rect(10, 10, position.width, 30), "TEST");
}

Feeling pretty dumb right now…:smile: