- Home /
Variable values not updating when script edited
I have several public static variables in a C# script, which aren't listed in the inspector. When I edit the script in MonoDevelop to change the values of those variables, the new values aren't being reflected when I run the game in the editor, although any other changes I make to the script at the same time work as expected.
Apparently the values of public variables are cached by the Unity editor when it loads the scene, overriding whatever changes I make to them in the script from that point on, until I shut down the editor and restart it.
I removed the [System.Serializable] from above the class definition and tried using [System.NonSerialized] on the variables within it, but that didn't make any difference. As noted above, none of these values are listed in the inspector anyway, so I don't understand why they don't just always reflect what's in the script, as you'd expect them to.
Is there any way to stop Unity overriding variable values like this, or to force it to flush whatever hidden cache it's pulling them from without having to restart the editor or reload the project every time I make a change?
NOTE: I can't really change the variables to private or non-static, or set their values in the Start script instead of where I declare them, as I've seen suggested in previous threads, as they're being used to initialise variables in other scripts for export to an external file.
Any help would be much appreciated.
The "caching" is done so that users can modify the values in the Inspector. There is a gear with an arrow on the right edge of each component. In the inspector you can select "Reset" to get that specific component to reset its values to the current ones defined in the script. Also you could initialize them in Awake(). That way they would be available in Start().
Thanks, but I tried using the Reset option (and even removing the script component from the object entirely and putting it back again), and that didn't work either. Also, as noted above, these variables don't actually appear in the inspector, so there's no reason Unity should be caching them really.
I've seen NonSerialized working as expected in a fresh script on another person's machine, and there's a public static variable in one of my scripts I'm having problems with that updates properly when I set it to be NonSerialized. That variable used to be a float, but I changed it to an array of floats this morning, which presumably would have flushed or invalidated whatever data Unity had stored for it. All the other public static variables in that script (and another one in the same scene) don't update though, even when I set them as NonSerialized.
So it may be a data corruption issue. Does anyone know if there's a way I can find and flush / delete whatever cache Unity is using for this and see if that clears the problem?
I missed the boat on my answer. I've converted it to a comment so you are more likely to get an answer. It sounds like a Unity bug.
This is still happening. I've been trolling the forums and question boards for hours looking for a solution. There is none. Shut Unity down and restart it -> Nothing. Shut $$anonymous$$onoDevelop down and restart -> Nothing. Change Variables to private and back -> Nothing. Reset the Script component -> Nothing. SHUT DOWN THE ENTIRE CO$$anonymous$$PUTER AND RESTART -> NOTHING.
UNITY SI$$anonymous$$PLY WILL NOT SHOW THE CHANGED VARIABLES FRO$$anonymous$$ $$anonymous$$Y SCRIPT IN THE EDITOR AT ALL.
I know Unity probably works perfectly well for you and you think you have a solution, but this is a bug in Unity.
Answer by ctwheels · Oct 28, 2014 at 09:44 AM
I know this is like a year later, but you can set the variable in question to be private, save your script, then change the variable back to public and save it again. It worked for me.
So,
public float myNum = 10;
to
private float myNum = 10;
and back to public after you save it.
In Unity 4.6 this doesn't seem to work for me. Gear > Reset did, however.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Serialize Variables from Items in an Array 1 Answer
Reference to value type 1 Answer
prevent field json serialization in 5.3 0 Answers