difference values between visual studio code and inspector

Hey everyone; I have a curious problem.

First (I thing is necessary) I’m learning Unity; yes, Im new.

But well; I made a Script for the player object in the game, but I noticed that there are differences between the attributes values in the Script and the Inspector. And the changes in one (For example: changing the value in C# code) dont make changes in the other (Inspector, in this example).

Once you add a component to a game object in the inspector, any serialisable values will be serialised (ergo, saved), and changing the code won’t change the values that already written to disk.

Basically, the ‘default values’ you set in code (otherwise known as field initialisers) are only used when adding the component.

Once the component is added you’re expected the change the values through the inspector.

1 Like

At least when you want them to be serialized :slight_smile: If you don’t want them to be serialized, they shouldn’t be public or marked with NonSerialized so they are not serialized at all. Though not serialized also means they don’t show up in the inspector, of course.

WELCOME! Unity is the best game engine in the entire world. Dive right in… you’ll love it.

This is why:

Field initializers versus using Reset() function and Unity serialization:

https://discussions.unity.com/t/829681/2

https://discussions.unity.com/t/846251/8

Serialized properties in Unity are initialized as a cascade of possible values:

  • what the class constructor makes (either default(T) or else field initializers)

  • what is saved with the prefab

  • what is saved with the prefab override(s)/variant(s)

  • what is saved in the scene and not applied to the prefab

  • what is changed in Awake(), Start(), or even later etc.

So you want to make sure you only initialize things at ONE of the above levels, or if necessary, at levels that you specifically understand in your use case. Otherwise errors will seem very mysterious.