Variables not setting correctly


I’ve got a few variables

public int health; 
private int test = 10;

Changing it in the Start function

public void Start()
    test = health;
    Debug.Log("test: " + test);

Which displays: 1, as intended. But later when I try to log the variable again, I get an incorrect number.

public void Touch()
    Debug.Log("test: " + test);

Displays: 10.

If I change this variable to 20. The Touch function will still display 10, until I re-open Unity.

What is happening…?

Since your health variable is empty, the test variable doesn’t know what to be set to, so you’ll need to enter a value in the health variable for it to work.

I ran into a similar situation just now. It turned out I was only changing my public variable on a single instance of a prefabbed object in the editor, when I wanted to change it on the prefab itself, leading to a lot of confusion. Is this perhaps what you’re doing as well?