Unity is not reflecting changes in script

I have tried using both visual studio 2019 (Community) and VS code, but whenever I make changes in a global variable, unity does not reflect that change. I have to reset the script component to reflect the changes. “Auto-refresh” is also checked in the preferences. In previous projects, it was working just fine.

If you have a variable set to public, it can only be changed through the inspector or at runtime through a script. For instance, if your variable looks like this:

public float variableName = 25f;

it will show up in the inspector in unity. You can then change it in the inspector. But if you try to change the line in your script it‘s value won’t change since it can be changed through the inspector. The value in the script is only the default value for new components.

One solution to the problem is to use HideInInspector. For instance:

[HideInInspector] public float variableName = 25f;

This won‘t show up in the inspector and the value will change if you modify the script.

Another solution is to just change it in the inspector instead of changing it in the script.