- Home /
Static variables assigned in the inspector
Several individuals have brought up the fact that static variables do not appear in the inspector. This is not exactly the same issue, but is very closely related "Is it possible to show Static Variables in the Inspector?" solutions given so far are all very interesting, but not one of them is an actual solution, they are work arounds. The problem is a need for a static variable a monobehavior, however that is achieved, that can be accessed in the inspector to change it regardless of which instance of that monobehavior you have selected and edited. This is not helpful if the solution only works at runtime, you need to be able to edit the before you hit run as well.
Now, Unity provides the wonderful scripting option to write your own inspector window GUI in which i have provided slots for my static variables. These function exactly as one would expect, you edit gravitationalScale in one instance and it changes in all instances. It even saves the data for when you save the scene. It however hits and all mighty bump when you tell the project to run. Apparently the value of static variables doesn't actually matter till runtime, because every time one hits run all of the static variables that have been redefined in the inspector snap back to whatever their defaults were, making the effort to assign them in the first place feel rather futile. To further frustrations, unlike all other variables in my inspector window, when the scene stops running the static variables all remain as they were just before run was ended. Even with this they return to their default variables after the scene is restarted.
Having attempted the singleton solution that many seem so proud of, it functions beautifully, though only does so while the scene is running, essentially leaving the same problem as with static variables. An attempt was also made to keep multiple copies of variables and check when they get changed to change the internalized static variables, but these methods(denied me thrice) also only truly function during runtime.
Thought this to be a simple problem, but either I am missing something or no one has actually solved this problem. If all else fails there is a fourth method, where one creates a master variable object to store all of the variable data that would be static. This then could be edited in advance, in runtime, or across scenes. It, however, creates a new issue. If at any time there are two controller objects in the same scene then depending on how you locate them and acquire data from them you will either get different data back in different scripts in an uncontrolled fashion, or you run the risk of editing the one that no scripts are checking. Do not consider this a solution, consider it a work around...
I would be happy to hear ANY new solution to this problem someone could offer, even sloppy solutions that I have not thought of yet may lead me to a true solution.
Answer by Mike 3 · Dec 25, 2010 at 04:59 AM
The singleton solution should work fine as long as you're manipulating the variable on the initial instance in the loading scene.
The issue with that is that you have to load that scene first, potentially making debugging single scenes harder
An alternative to that would be to make a prefab with the correct values set in a script, and have a single instance of that instantiated as the singleton if the instance is null. You would just need to put the prefab instantiation object into every scene, and it'd be fairly easy to maintain
Regarding why static variables aren't saved - unity makes no attempt to read the static variable for serialization (and deserialization when a copy of the script is initialized). I assume that it's because frankly it would be a little ridiculous; if unity changed the static variable to the serialized one when it instantiated the first copy of the script in the scene, what would happen if you had already modified the variable from another script?
Answer by Ziron999 · Oct 24, 2016 at 09:01 AM
This may be old but i have a good example of why this is needed...Ad version of APP plus non-ad version of APP. I want to make it static for when it's true to be used in other scripts. I have to manually change it to true every time i copy over the singleton. A check mark would be faster. Overall this is not a solution in fact, there isn't one. This needs to be a request to the development team with examples like this as to why it needs to happen.
Your answer
Follow this Question
Related Questions
Inspector top not working properly 0 Answers
Unity Editor - Class variable value contrain 4 Answers
Custom inspector for specific variable type to be reused? 2 Answers
Moving the input fields in a custom inspector 2 Answers
How does one inspect static vars? 3 Answers