Inspector not showing my initial desired value

I declared my variable on my code, named it moveSpeed with the value of 10, for example, and I also serialized it so I can change it through my inspector. When i go to unity, the box where I can change the moveSpeed value shows 0, not my desired inital value which was 10.

Does Unity automatically put my serialized values to 0? And if so, can I change it somehow to get my initial value from the one referenced in the code?

Thanks

Hi,

When exactly did you set the moveSpeed value to 10? If you did that after you assigned your script (with the variable) to the Inspector, the Inspector gets control over the component. The default value of an exposed variable in your code is only relevant at the moment when you attach the script to an Inspector. The Inspector uses that default value as the initial value but won’t change it anymore just because the script got modified.

This behaviour makes sense because, otherwise, it might be that you spend hours fine-tuning values in the Inspector for multiple game objects which have the same script component attached, and after you changed something in the script component, all values would get reset to the initial values. That would be a nightmare because you would have to fine-tune everything again—each time you make a change in the script.

Did this help?


See also:

1 Like

I am having the same issue as OP. I think my code is correct as per you reply but here it is in case i didn’t do it fully correctly.

If you have
[SerializeField] float moveSpeed;

and you go into Unity, it’ll compile it with the default value of 0f.

If you have
[SerializeField] float moveSpeed = 10f;
and you go into Unity, it’ll compile it with the default value of 10f.

If you do the first one first (0f), it’ll default to 0f. If you then change the code to the second one (10f) the inspector has already defaulted to 0f and won’t change, so you’ll have to manually change it.

It never did for me. My friend said jut copy the code into a new script which worked

Yes it did… But if you want to continue to make new scripts rather than learning a Unity fundamental, I guess that’s your choice.

1 Like

This is really not the way to do things, the fact your friend suggested it is concerning.

Serialized fields can be reset from the inspector.

how can they be reset?

ResetImage

On the script that holds the serialized field, left click on the three small dots, in the drop down you will see reset.
This will reset all the values in your serialized fields back to what they are in your base script.

OR

Right click on the label for an individual field and click revert, this will reset just that fields value to that in your script.

Hope this helps.

@ verysmallpingu This also answers your question mate.

3 Likes

Privacy & Terms