How to make my scene object reference survive the button play

Hi,

I did the tour of Google and Unity resources, I just can’t seem to find an answer to my problem.

Basically I want use an EditorWindow to generate and manipulate some graphs tree that are then evaluated during the game. I generate those trees in the scene directly I may sometimes save them separately into a prefab but most of the time I just keep them into the scenes.

My drama as a lot of people had starts when I click play. The evaluation of the tree happens correctly but the visual of the tree I had in my custom editor window just vanished because it loses the reference to the scene object. So if I want to have this visual I need to redo the link manually on play time which is annoying.

Here is a sample to illustrate the same issue I basically have:

[System.Serializable]
public class Data : MonoBehaviour
{
	public int difficulty = 0;
	public string description;
}

public class MyWindow : EditorWindow 
{
	[SerializeField] // To be sure it is serialized
	public Data data;

	// Add menu named "My Window" to the Window menu
	[MenuItem ("Window/My Window")]
	static void Init () 
	{
		// Get existing open window or if none, make a new one:
		MyWindow window = (MyWindow)EditorWindow.GetWindow (typeof (MyWindow));
		window.Show();
	}

	void OnEnable()
	{
		hideFlags = HideFlags.HideAndDontSave; // Tried that
		EditorUtility.SetDirty(data); // that too
	}

	void OnGUI () 
	{
		data = (Data)EditorGUILayout.ObjectField(data, typeof(Data), true);
	}
}

And here is my scene, where GameObject have Data attached to it and GameObject (1).
71116-scene.jpg

Whichever the one I use, the ObjectField keeps getting reset on play. While if I use the prefab of GameObject in the project panel it doesn’t. But it isn’t the prefab that will be running in my scene and that I want to debug. It’s the real instance in the scene…

Am I trying to do something that cannot be done?

Thanks.

Been able to fix that issue with the solution given by @dkozar here

http://forum.unity3d.com/threads/editorwindow-loses-reference-of-scriptableobject-on-play-mode.107831/#post-1077162

\o/