- Home /
static class not keeping changes after playmode
When I'm editing my static class using an editor window it all works fine. But when I enter and exit play mode my changes are reverted. How can I fix this?
Answer by Bunny83 · Apr 12, 2013 at 08:36 PM
You can't "fix" this because the whole scripting environment is reset when you change playmode. Edit-time and run-time are two different worlds and static variables shouldn't be used as "permanent" memory because it isn't permanent. Static variables aren't serialized. Use non static variables of a serialized object like a MonoBehaviour or ScriptableObject-asset. If you just want to store values for your editor window, use EditorPrefs.
What I want is the script to be able to save it's data without having to be attached to a gameobject.
If it's an editor script and you only want to save simple data (int, string, bool) use EditorPrefs. If you have more complex data you might want to save your data as asset by using a ScriptableObject and the AssetDatabase class.
Anyway, if you "just" want to preserve the data of an editor window when you change between edit and runtime, just make your variables public and non-static. Editor classes are also serialized, but they aren't saved to disk
@Bunny83 If you have a menu item that needs to manipulate a complex object, your latter solution won't work as far as I know, since menu items must be static, and statics can't access non-statics.
@vargonian: I'm not sure what you're after. $$anonymous$$enuItems are implemented with static methods, yes, but it depends on what you want to do inside that method. You can always use GameObject.Find / FindObjectOfType / GetWindow / Resources.Load + Instantiate to get a reference to an instance of a serializable class.
You might want to implement a simple editor window since editorwindow instances have their public members serialized like $$anonymous$$onoBehaviours. Also you should be more specific what you mean by "complex". What kind of object do you talk about? Where is that object stored / referenced / used?
You probably should ask your own question with some of your code if you want any further help.
I actually did ask my own question today: http://answers.unity3d.com/questions/705165/how-to-prevent-a-menu-operation-from-cancelling-wh.html
The object I don't want to die is not a Unity object and not a basic type. It is a really standard C# class, though. $$anonymous$$y menu item method creates an object which subscribes to the events published by that statically defined class, so if that object dies (on an editor reload), the menu item can't do its job because the event publisher will be destroyed.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Request Help: Sprite Editor not Automatically Slicing Sprite Sheet 0 Answers
Editor Button Rotates Selected Object 1 Answer
Fatal Error While Playing -- VertexData.cpp 0 Answers
Too many bugs 0 Answers