- Home /
Public variable in script doesn't update in Unity inspector when saved
I'm new to Unity and am trying to build a very basic script. I have a property in a game control script like so:
public float objectSpeed =0.1f;
If I save this script and attach it to an empty game control object, I can now see the script attached in the inspector showing the property, which is called Object Speed:
But if I change the value of objectSpeed in the script and save the script, it doesn't update the value in the inspector in Unity, meaning that when my script runs, it picks up the value from within Unity, and not the updated value in the actual script.
I've tried saving the script, restarting Mono and Unity, and the original value never changes. However if I remove and re-attach the game control script to the game control object, the value now successfully updates in the Unity inspector.
I'm not sure what is going on here and this has caused me some confusion. Is this a bug or am I doing something daft? I'm using Unity version 4.5.2f1.
I'm also experiencing this (or at least something similar). $$anonymous$$y workaround is that when I hit Play, I stop and hit it again and it finally updates.
Answer by Landern · Aug 28, 2014 at 01:47 PM
If it was already attached in scene or on prefab, the serialized value originally in the inspector gets saved. If you want to reset the component to the script default values, click the little cog(gear) and reset the component.
Ah yes that fixes it. Caught me right out, useful to know! Thank you!
Is there a better way to do this? Lets say in the case of myself having 10 of these objects that won't update?
Couldn't you just ctrl+click the ten different objects and set their values in the inspector all at once? Alternatively, you could try to comment out the variable in your script and all of it's usages, save the scene, then remove the comments.
Yes if they all have the same script you could, but still if you have lots of objects, and a few different scripts that need some number to change this could be really annoying.
also resetting the script remove other things you might have added to it so you will need to add again anything you have on that script (like game objects).
It would really be the best if changing the script actually changed it on everything.
Your answer
Follow this Question
Related Questions
How to get a value set by another script? 0 Answers
Text in the scene is not being updated! 0 Answers
what's wrong with unity 1 Answer
How do I add a tool tip for class variables in the inspector? 1 Answer
Specifying script through inspector 0 Answers