- Home /
Checking variable each frame to increase another variable every few seconds
I'm having issues getting one variable to update in response to the value of another variable.
Eg
Function Update () {
while (X > 0.9) {
Y --;
yield WaitForSeconds(2);
}
}
My problem is that I need to be checking X every frame, but waiting a couple of seconds before reducing Y. Obviously I can't have this in the update function due to the yield.
Invoke repeating wont work because X fluctuates based on actions the player takes.
The logic of the code is simple enough, but I've got no idea where I should be placing it so I don't end up in an infinite loop, or having the code execute instantly.
Answer by spinaljack · Aug 18, 2010 at 12:09 PM
Use a time stamp instead:
var lastUpdate : float = 0.0;
function Update(){ if(x>0.9 && Time.time-lastUpdate > 2){ lastUpdate = Time.time; y--; } }
Well I feel like a bit of a dunce. I'm sure I tried using the time stamp idea and had issues with it. But it's working now. Perhaps it was some other code interfering...either way, thanks :D