Defaultize a variable in the inspector

I have an integer field in my inspector. I have assigned a default value in the script on initialization, however the value is ignored by Unity, because sometime back I set a value to its field in the Inspector. Since then setting the value to 0 or simply deleting it and unfocussing the field doesn’t delete the value.

With GameObjects and other item component it would be easy by selecting “None”, but this one doesn’t have such slider.

I understand I could reset the entire component, but its going to get problematic if I have to do this every time I want Unity to ignore Inspector values. I also could delete the variable, and then set it again, but that is a workaround, not a solution. I also could set that value in Awake() but once again, that’s a workaround.

What is a proper way to “deset” a value in the inspector, so that my scripts are once again deciding what the variable’s initial value should be?

Hi @a7777777777 ,

The values that you’re observing on the Inspector window, are coming from an already setup instance of whatever components you attached to the object that you’re checking the values on such a window.

If you change the default value on your script, this will not change the values for the already created instances, because this default value is to be applied for new instances of your objects.

What you can do is to create an Editor script that you can use for setting the value to the new default without having to do it manually, going one-by-one in your already created objects.

My recommendation is to create a MenuItem with the functionality that you want. You can find more information about it here: Unity - Scripting API: MenuItem

Good luck with your project!

1 Like