Inspector not updating value changes

So my problem is that if i change a value of a variable in the source code and save it doesnt update in the Inspector. I know it updates the values when i hit reset on the script in the Inspector, but i see that as an inconvenience.

On the other hand, if i declare a new variable with a set value, the Inspector has no problems to add them correctly.

So basically if i have this:

var speed : float = 3;

and change it to

var speed : float = 5;

it should show up on the Inspector, without having to manually click on Reset.

Robertbu is correct in saying it is by design. This happens because, apparently, the serializedObjects corresponding to our scripts are not updated (at least from values from the field initializers, which are what you’re using) after an assembly reload. SerializedObjects are what Unity uses internally to store data which will not disappear during a switch to play mode or during an assembly reload.

Like Robertbu says, public is for inspector, private is code. If you make a variable public, you should modify it from the inspector (when not in runtime, that is. What happens runtime, stays in runtime…). If you make a variable private, you should modify it from code.

There are three ways I can think of to get around this design:

  • Make a custom inspector, and set the value from there, in e.g. OnEnable
  • Add the ExecuteInEditMode attribute to your script and initialize everything in the Awake method. There are consequences to ExecuteInEditMode which may or may not be wanted.
  • Make a constructor, wherein you set the values. (Not recommended in the Unity documentation)

But why get around a good design? Just declare the variable in script, and do all further modifications (setting starting values etc.) in the inspector. For reference types you may have to resort to the Reset method. Set your knife damage too high? No problem, just modify it from the inspector. This eliminates the need to even touch code and cause assembly reloads, which may take a while when your codebase is big.