- Home /
How to subtract 1 from an int each second?
The title explains it all. How would I code a function that subtracts 1 from an integer each second?
Answer by Bunny83 · May 15, 2018 at 08:01 PM
Like this:
int someInt;
void Start()
{
InvokeRepeating("Subtract", 1f, 1f);
}
void Subtract()
{
someInt -= 1;
}
Answer by Topthink · May 15, 2018 at 08:02 PM
Try "InvokeRepeating" I think the 2.0f is for the delay and then the .3f is for repeating every .3 seconds (or the opposite), you'd need to check that for certain. But basically, it calls the function every .3 seconds after a certain delay and does whatever the function asks...in your case you will subtract 1 from an integer.
void Start()
{
InvokeRepeating("CallThisFunction", 2.0f, 0.3f);
}
void CallThisFunction()
{
Here, you would subtract one from the integer.
}
Of course, you put your own time requirements in...whatever the delay is and so forth. I see Bunny beat me to it ... great $$anonymous$$ds think alike I guess. ;)
Your answer
Follow this Question
Related Questions
timer not ticking down 2 Answers
How to make a time based game? 2 Answers
How do I make a Countdown Timer in Minutes and Seconds only 1 Answer
Timer Continues After Object is disabled 2 Answers