- Home /
Setting static variables in Editor script
I need to set some variables in one of my runtime scripts, but I need to do it from an Editor script that is targeting a different script.
For example: DataScript + DataScript_Editor needs to set variables in RuntimeScript.
Can I do this simply having the RuntimeScript vars as static? Would assignment of static properties persist through playing & stopping the game, quitting Unity, all the way to publishing builds, and they'll never change or reset unless I explicitly reassign them? — Is this what static vars are for? — Or is there some conditions where a static var will be reset?
Answer by fafase · May 28, 2014 at 07:58 PM
Static is not meant to be a variable to live for the whole extend of a program, it just happens to do so but this is in no way why it was defined.
Static is a variable that belongs to the class while others are instance members. The reason why it lives through the whole project is because it belongs to the class and not to any instance objects. As a result whether there is an instance or not, the static is there since the class is just a mold for object and is always there.
Like the count of enemy is a candidate for static as you need to keep track of how many of them and if none left you still need that value for later when you create new ones.
Nonetheless, whatever you do with a static can probably be done without it.
Static is like any other variables, stored in the ram which is a volatile memory (values are just electronic impulse) so when you put your computer off all is lost. For this you need to store on the ROM via some text file or player prefs class from Unity.
Finally, static variable do not show in the inspector since they do not belong to the object but the class (I start to repeat myself). So the inspector do not show it.
Still it is possible to create a public instance variable that will set the static in the start or later. Other way is to use an Init(value) to set the static when needed.
Thanks for the clarification. I noticed static variables seem to persist between sessions but if that's not expected behavior, I obviously cannot rely on it.
I will find a workaround. Thanks!
it is expected behaviour, it is just not what it was designed for. The nature of static makes them persistent. The big thing is what are you after exactly and maybe static would be a candidate but it seems to be more of a external storage system.
I'm setting up my gameplay properties for each of my game's levels (e.g., time limits, enemy numbers, objectives, etc.), which is currently input and saved via the inspector of a 'LevelData' prefab object which I instantiate at runtime. Some properties are calculated and then populated automatically via an editor script. — This works great, but now I need to automatically adjust a property on an unrelated object. I could just reference this object from the original prefab, but I wondered if I could just set it via a static variable.
It is not because I want to say this is wrong, but if this is the case, why does static classes exist? (And sorry if this is a bit late to ask.)
There are reasons and then there are reasons, $$anonymous$$icrosoft is good enough to detail their thoughts on the subject -> https://msdn.microsoft.com/en-us/library/79b3xss3.aspx
Answer by cryingwolf85 · May 28, 2014 at 06:58 PM
The actual use of static variables is a variable that extends across the entire run of the program. Whether you use a static variable or pass an object around has no effect on when the variable is removed from memory.
In your case using a static variable would be incorrect. When you stop running the program it will be removed from memory (as will any other type of variable).
I feel that the function DontDestroyOnLoad may be of use to you. As for going across builds, you can check out something like PlayerPrefs. You're going to need to save the data externally (Example: Database/file).
Good luck.
Thanks for the help. I picked fafase's answer as it went into a bit more detail about static vars but thanks for the suggestions. I think what I need to do is change the variables in the prefab, that way the values will stay set.
Answer by JohnPoison · Jun 21, 2016 at 09:27 PM
Currently, for storing data between editor sessions you can use ScriptableObject. Notice that you can't modify it in runtime inside a player, only in editor. But I think it fits to your requirements.
Your answer
Follow this Question
Related Questions
Static Variable Problem 1 Answer
Public and Static Variables 2 Answers
Is it possible to show Static Variables in the Inspector? 10 Answers
Increasing a score value? 2 Answers