- Home /
Increment X every one second.
I have a program where every tick of a clock there is a corresponding amount of time. Here for every 1 minute it is equivalent to 5 hours (simulated time), but I have a hard time where the problem exist.
function count_coroutine()
{
var seconds : int = 0;
var second_sec : int = 0;
var second_min : int = 0;
var second_hour : int = 0;
while(true)
{
for (var timerz : float = 0; timerz < 1; timerz += Time.deltaTime)
{
second_sec += (300/60);
if (second_sec >= 60)
{
second_min++;
second_sec -= 60;
if (second_min >= 60)
{
second_hour++;
second_min -= 60;
}
}
yield null;
}
seconds++;
textMeshReal.text = seconds.ToString("00");
textMeshSim.text = second_hour.ToString("00") + " : " + second_min.ToString("00") + " : " + second_sec.ToString("00");
}
}
The expected output is 1 $$anonymous$$inute (text$$anonymous$$eshReal.text) = 5 Hours (text$$anonymous$$eshSim.text), but the output is 1 $$anonymous$$inute = 5:04:55
The problem might be that you are adding and substracting to three different variables (I haven't checked if your math is correct). Why don't you just increment the seconds and figure out the $$anonymous$$utes and hours from them?
Besides, do you know that yield can be used with a time gap? You can use seconds. Check here.
In this code, no overflow in deltaTime is being carried over from the previous iteration.
@$$anonymous$$: so what do you mean? can you please explain further because I'm still a noob at this things.
Answer by $$anonymous$$ · Aug 21, 2014 at 11:46 AM
A simple solution would be to update a variable of time passed given Time.deltaTime.
var hoursPassed : float = 0
function update()
{
hoursPassed += Time.deltaTime * 50 / 60;
}
That is, 50 hours pass for every 60 seconds. Sorry if the js, has a typo or w/e, I'm a C# programmer.
If you want to update only once a second, check out alishka's answer.
@$$anonymous$$: is hoursPassed += Time.deltaTime * 50 / 60;
is equal to "1 $$anonymous$$inute = 50 Hours"?
Answer by alishka · Aug 21, 2014 at 10:33 AM
You need to use waitforseconds()
in the yield statement and get rid of the for loop.
i used waitforseconds(1);
and the output is 1 $$anonymous$$ute = 00: 05: 00. The OUTPUT $$anonymous$$UST be 1 $$anonymous$$ute = 05 : 00 : 00.
Almost there ! Now you might want to change the way you increment the game time :) You waited 1 second and the game time got incremented by 5 $$anonymous$$utes.
Off the back of my head I have this:
while(true){
//wait 60 sec = 1 $$anonymous$$
yield return new waitforseconds(60);
//increment
gametimehours += 5;
}
but i want in every 1 second an update (per second update). Like 00:01 = 00:00:05 00:02 = 00:00:10
Your answer
Follow this Question
Related Questions
changing a coroutine's argument at the coroutine's runtime? 1 Answer
Keeping track of time? 4 Answers
Updating a TextMesh buggy? 1 Answer
A node in a childnode? 1 Answer
Help scrolling puzzle is not. 0 Answers