- Home /
Time.timeScale problem
So, i creating a popup GUI that will show a countdown timer, but i need to make the scene behind the popup GUI standstill.. i've been searching awhile but i don't get anything that could satisfied my need.. here's my code for the process..
function OnGUI() {
if(GUI.Button(new Rect(30,350,80,80)," ",tombol1)){
popUp = true;
Attack1();
if(enemy_hp<=0){
enemy_hp=0;
MonsterIsDead();
}
if(player_hp<=0){
player_hp = 0;
PlayerIsDead();
}
}
else if(GUI.Button(new Rect(90,280,80,80)," ",tombol2)){
}
else if(GUI.Button(new Rect(130,350,80,80)," ",tombol3)){
}
if(popUp){
Time.timeScale = 1;
GUI.BeginGroup(new Rect(208,97,350,350),soal);
GUI.Label (new Rect(100,20,50,50),seconds.ToString());
InvokeRepeating("Countdown",1.0,1.0);
GUI.EndGroup();
}
}
function Countdown(){
if (--seconds == 0) CancelInvoke ("Countdown");
seconds = parseInt(seconds.ToString());
if(Input.GetKeyDown("a")){
CancelInvoke("Countdown");
}
}
so i tried it and when i encounter the GUI, the counter does counting down, but in hyperspeed, i am not sure what happening there.. but if i set the Time.timeScale to 0, the countdown timer just not counting.. it really confuse me, what should or could i do to fix this kind of problem?
What rutter said. It seems like you might be calling invokerepeating in an update function. For example in "if(pop up){}" you call for InvokeRepeating(line 26). If "if(pop up){}" is running in an Update function it wont count correctly as it'll get updated called every frame(Update gets called a ton load of times a sec, that's why your timer runs way too fast). I always put invokeRepeating outside of any update functions and it works the way I want.
@superluigi aww, didn't see your psot back then.. yeah i understand it.. but i need it to be there, is there no other way to fix it?
i want to show the timer on this GUI and counting down only when this GUI popup.. its ok if it will be an GUIText or GUI from script.. as long as it only show up when the popup comes..
i've been searching for awhile but really, i couldn't find how to do it.. i know there'll always be some way to achieve this, but maybe it's because i lack of algorithm logic that i still don't know how to do it..
Answer by rutter · Jan 22, 2014 at 01:09 AM
I see two problems:
First, it looks like you might be calling InvokeRepeating
once per frame while the popup is active. That's going to cause quite a few calls to Countdown
, and that's why your timer was moving too fast. Your script assumes that each call means one second has passed; that's only true if you call it once per second.
Second, setting Time.timeScale
to zero will freeze time completely. It's easy to overlook some of the implications. Among other things, coroutines and invokes that rely on conventional time passing will stop, and that's why your timer was stopping. Animations and audio that are tied to time will also stop. There are some popular workarounds which are mentioned in this thread: Freeze game using Time.timeScale = 0. Most involve some clever use of `Time.realtimeSinceStartup`, which is not affected by timeScale
.
From the workarounds thread, you might want to try the very small timescale, or pause the game completely and switch to coroutines.
yeah, i wanted it to pause completely but the GUI timer still moving with normal condition.. but after u pointed me the problems, i'll try to see what can i do about it..
and also. Time.realtimeSinceStartup
could move only the timer i made up there? or it is something that will move(counting) from Startup of the scene?
Thanks @rutter
EDIT :
ok, i figured out that i don't need time.TimeScale = 0;
will there be any way to still make the time move as slow as 1 seconds? or maybe is that possible to Invoke the countdown on the outside of GUI but the text inside the GUI "changing" as the seconds?
Your answer
Follow this Question
Related Questions
C# countdown timer 9 Answers
How to stop a timer with GUI button? 1 Answer
Countdown Timer 1 Answer
Problems in c# countdown timer 1 Answer
3, 2, 1 GO! 3 Answers