How do I do math in unity.(timer/60 and timer%60)
Right now I'm trying to get the seconds from a timer and then format it into minutes and seconds. So I though doing this:
timer += Time.deltaTime; int min = timer/60; int sec = timer%60;
But I don't know how to do it in unity.
Answer by Ahsenhein · Mar 05, 2017 at 10:26 PM
I'm doing this but I don't know why the timer isn't acceptable on the math operations there.
timer += Time.deltaTime;
Mathf.FloorToInt(timer);
int min = timer / 60;
int sec = timer % 60;
text.text = min + ":" + sec;
and now I have changed the code and It's working but I don't really know why it wasn't working before.
timer += Time.deltaTime;
int min = (int)timer / 60;
int sec = (int)timer % 60;
text.text = min + ":" + sec;
first time you altered timer by flooring it. it never got bigger because deltatime is usually too small to increase to the next int.
second time you didn't.
Your answer
Follow this Question
Related Questions
Milliseconds Timer Question 1 Answer
Using Time.deltaTime as a Timer 0 Answers
Load scene after time 1 Answer
How is it possible to verify values and is they are correct activate objects ? 0 Answers
20 minute countdown timer 2 Answers