Why does initialized value not copy to Unity UI?

I’m struggling to understand how a SerializedField works. For example, if I initialize a value in the script like so:

[Range(0.0f, 5f)] [SerializeField] private float walkSpeed = 0.5f;

Unity will accurately show the initialized value:

image

However, if I make a change to my script and initialize it at 0.8f instead, for example, the 0.5 value remains in Unity and that is the value that is used when playing the game.

Is there a way to tell Unity to revert to the Script value instead or am I missing a standard behavior and reason that it behaves this way?

I believe this is an expected behavior. The inspector overrides the script default value, and changing the script will not override the inspector unless the inspector value is out of range or something.

1 Like

Hi Zephyr,

@Bryant_Schaper is right. I believe the reason for this behaviour is that Unity is not supposed to override any values in the Inspector just because something was changed in the code. Otherwise, if you had various Attacker components with different speed values, all values would get reset even if you want to keep them.

If you want to reset a component to the values in your code, click on the cogwheel icon, then on “Reset Component”.

Did this clear it up for you?


See also:

1 Like

Privacy & Terms