Why does not variable change?

Hi! I have code:

        public float randF = -12.0f;  
        bool ToF = true;

        void Update() {
    
            if (ToF) {
                Debug.Log(randF); 
`              ToF = false;
            }
         }

I press “Save” in VS and “Play” in Unity.
115315-01.jpg
Then I change variable: randF = 12;
Press “Save” and result does not change.

-12
UnityEngine.Debug:Log(Object)

Only when I change “public” to “private” or “private” to “public”, result changes.

Why?

Because once Unity serializes (saves) the values, changing the initialization value when declaring the variable won’t affect the editor.

You have two options:

  • Change the value in the inspector
  • Change the value when declarign the variable, and then, hit the Reset button inside the menu of the component (top-right corner of the component you will find a little cog)