Custom EditorWindow no longer linked with game object after leaving play mode

Here’s the senario:

  • connect the game object to the editor script by dragging it to a custom ObjectField
  • The custom editor creates several checkboxes that get reflected in the game object’s inspector view
  • Set the checkboxes I want reflected in the game object (this part works fine)
  • enter play mode (everything here works fine with the expected flags set from the checkboxes)
  • stop play mode
  • Attempt to change the check boxes again, the change are no longer reflected in the game object’s inspector view

All of my custom scripts are already marked as serializable (hence why it works on the first play). I’m also marking the game object dirty in the onGUI function. I tried accessing the game object via instanceID with the same result.

I’ve read about the possibility of saving the state via asset, but since I can’t seem to access the object to update it so I’m not sure this gets me anything.

Any other suggestions?

To update, it looks like the reference to the object was held but the components were not.

I ended up just adding a GetComponent call on the game object to the OnFocus method. Since I only directly interact with one of the components in my editorwindow I’m able to just get that single component and the dependency chain from there on appears to be intact.

It has to do with the way the classes are serialized and deserialized. They are not deserialized as references like they originally were. You can solve this by inheriting from ScriptableObject. Watch Tim Cooper’s Unite Nordic 2013 demo on Serialization… he discusses and shows how to fix this very issue (without using GetComponent):

http://www.youtube.com/watch?v=MmUT0ljrHNc