Serialized Fields never change in the Inspector

I was watching Sam’s EXP change in the Inspector during this lecture and so when I replicated it mine does not. I did a Debug Log and can see everything is working fine but my value isn’t changing in the Inspector.

I’ve also noticed in the past, if I set a value for a Serialized Field in the code, the Inspector version always hard codes over it. I assume there is a setting in Unity that can change that and was hoping someone could tell me whats going on

1 Like

Got code / screenshots to show us what you mean?

There’s a difference between
[SerializeField] int hitPoints = 3;

to

Start()
{
hitPoints = 3;
}

Where the former won’t update in the inspector, but the latter will.

There is no setting in Unity that lets code overwrite inspector values, and there never will be. This is expected (and required) behaviour. If I have 2 enemies - one with 10 health and another with 20 health - overwriting these values in code will make no sense. This is the reason inspector values overwrite code values.

If you change an object’s value in code, it will change in the inspector only during runtime. As soon as you stop the game, the value will return to whatever you set it to in the inspector before running the game. Also, this will only show on the actual game object. If you are looking at the prefab or a different instance, it will not show the change.

1 Like

Its weird. I booted up the PC and it was working. Basically what was happening was I was gaining EXP and the display in the top left was updating, but the value in the Inspector when clicked on Player just had the EXP set to 0.

But today it worked fine. So not sure what was going on

1 Like

Sometimes these things happen… scripts don’t update, etc… and often a restart fixes it.

2 Likes

Privacy & Terms