- Home /
Does Unity overwrite static values on play?
I have a static variable in a class, let's call it StaticClass.ExternalValue. In the editor, I have a button that does the following:
StaticClass.ExternalValue = "Some string! Hello."
UnityEditor.EditorApplication.isPlaying = true;
In other words, it sets the value of the static field, and then starts the game playing. The problem is that the act of starting the game playing seems to cause the static fields to be overwritten with their default values again. How can I pass data from the editor to the game in this way?
usually you need to mark things dirty when you change them via editor script to write the change to disc:
EditorUtility.SetDirty - http://docs.unity3d.com/Documentation/ScriptReference/EditorUtility.SetDirty.html
Not quite sure if can you can do that on a static class...
This is making the change before the game is ever run though, no idea if that affects it. Will try this now, thanks.
You may want to research ScriptableObject - http://docs.unity3d.com/Documentation/ScriptReference/ScriptableObject.html
Its a good way to store data for runtime, like string data. It allows you to create an asset that holds whatever data you want and store it in the AssetDatabase. Do a quick google search for some more insight. Hope this helps...
Definitely did help - great idea, will look into it.
Answer by Ashkan_gc · Jul 16, 2013 at 02:27 PM
As much as i know you have three ways to do that, 1- Use the EditorPrefs to store the value and read the value when playing in static constructor or Awake or somewhere else 2- Write the value to a file and read it back when you are playing 3- Put the values in a ScriptableObject and save them, then use the ScripableObject as an asset in your script.
The reason of overwriting is that unity simulates an executable running from scratch when you press play or go to play mode by code. You can not serialize static variables (not fully sure) so you can not tell the editor to save them.
Your answer
Follow this Question
Related Questions
Accessing GameObjects from Editor static class in Plugins...not. 1 Answer
Unity Editor Event System 1 Answer
Changes to Object made in custom Editor Window don't persist 0 Answers
Keep equal width for panels in EditorGUILayout.HorizontalScope 0 Answers
Execute editor window scripts when project errors are present 0 Answers