- Home /
User found the problem and fixed it
Strange persistent variable duplication
I'm thoroughly confused. I am attempting to have a script access and change a float variable like I've always done, but it didn't seem to be able to change the value. I then had both the script that was changing the float and the script that contained the float print the value of the variable that I was referring to, and they're printing two vastly different values. The stranger part is that one of the values will persist through stopping and reloading the scene, and even the restarting of Unity. Here's basically what I have:
Script 1 on ObjectOne:
public float value;
void Update (){
print(value);
}
Script 2 on ObjectTwo:
public GameObject objectOne;
void Update () {
ScriptOne one = objectOne.GetComponent <ScriptOne> ();
one.Value -= Time.deltaTime;
print(one.Value);
}
When I have both of these scripts running at the same time, they're showing completely different values. Script 1 shows the value that I set in Start of 200 and unchanging while script 2 shows the value at lower than -1,000 and decreasing. If I stop and start the scene again or restart Unity, script 2 will pick back up at the value that it left and continue to drop the value that it prints out.
Is anyone able to explain this to me? I didn't think variables were supposed to be able to persist unless explicitly made to. Any help is much appreciated!