- Home /
Adding a value to a variable every second
I have spent a while trying to figure this out and promised my last resort would be Unity Answers. But here I am.
I need to increase a variable by 1 every second. I've tried to do:
thirst += Time.deltaTime * 1;
but it just doesn't seem to work.
I'm not totally clear on some of this Time.time vs Time.deltaTime, but ins$$anonymous$$d of += I would think if you just changed it to = it would solve it. Because right now it seems to be exponential so the first second it adds one, then the second second when Time.deltaTime = two, it adds two, and so on so it's adding more every second. If it's just equal to Time.deltaTime *1 then after ten seconds thirst is ten, after twenty, thirst is twenty, and so on. At least that's what I would try next
Answer by AurimasBlazulionis · Jun 20, 2015 at 09:04 PM
There are quite a few ways. The first one, using while loop
var value : Boolean = true;
function AddSomeValue(){ //call this when you need to start adding
while(value){ //this lets you stop the loop at any time
//just change the value of this variable to false
yield WaitForSeconds(1);
thirst++;
}
}
Another, using invokerepeating
InvokeRepeating("AddValue", 1, 1); // function string, start after float, repeat rate float
function AddValue () {
thirst++;
}
you can cancel invokerepeating using CancelInvoke(), but if you have many of them in one script, it will stop all of them
This was a fantastic solution. It allowed me to "gradually" adjust the difficulty level over time.
Answer by Arkamis · Jun 21, 2015 at 11:17 AM
Time.deltaTime
gives you the time since the last frame. Computing thirst += Time.deltaTime;
will increase the thirst counter by the length of a frame, every frame -- hence, one unit per second.
If you are not seeing this behavior, chances are you are resetting thirst
somewhere, or you have done something to the Time scale. In other words, your code should work, unless there are bits of it you're not telling us about.
Try adding a log statement whenever you increment thirst
, and see how the values evolve.
Answer by zelalem_t · Feb 07, 2018 at 07:14 AM
int gameStartTime=(int)Time.time
void update(){ if((int)Time.time-gameStartTime==1){ //do something here gameStartTime=(int)Time.time } }
Answer by jobenjo · Jun 21, 2015 at 11:17 AM
Time.deltaTime is the amount of time it takes to load the last frame, so for example 0.2 seconds. Therefore, what you said above would be seen to the computer like this: thirst += 0.2 * 1; multiplying that number by one, doesnt change it in the first place anyways; so its normal it doesnt work.
The +=
operator operates by increment. thirst += 0.2 * 1
is the same as thirst = thirst + 0.2
. This will definitely increment thirst
by 0.2
every frame.
All I can tell you, guysh, is that he uses int, so that is why it didn't work lol
Well if he uses int
, it sure won't work, but the OP hasn't posted the code where it's declared.
@TheDiamondPlay The word int
doesn't appear in this post until your comment. It is not clear that the OP is using an int
vs. a float
. This is one possible cause. Another possible cause is that the OP is re-initializing thirst
at the beginning of every frame.