So i've been working on GUI for a while.. and trying to create a timer countdown, it's done and i could make it moving.. but the problem comes when i write it on Code (csharp): function OnGUI() here's my code Code (csharp): function OnGUI() { if(GUI.Button(new Rect(30,350,80,80)," ",tombol1)){ popUp = true; 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){ GUI.BeginGroup(new Rect(208,97,350,350),soal); GUI.Label (new Rect(100,20,50,50),seconds.ToString()); GUI.Button(Rect(50,280,50,50),"",buttonA); GUI.Button(Rect(150,280,50,50),"",buttonB); GUI.Button(Rect(250,280,50,50),"",buttonC); GUI.EndGroup(); InvokeRepeating("Countdown",1.0,1.0); } } function Countdown(){ if (--seconds == 0) CancelInvoke ("Countdown"); seconds = parseInt(seconds.ToString()); if(Input.GetKeyDown("a")){ CancelInvoke("Countdown"); } } and the error is, if i start the GUI, and the popup comes, the timer dropped incredribly fast from 60 to 0, it's like 1 sec or 1 frame.. i alrdy asked this matter on Answer and someone replying that the problem is the InvokeRepeating get called ever frames, and it means the Repeating must finish on 1 frame. But is there anyway to achieve this ?
Why not just: Code (csharp): static float m_timer = 0.0f; static float m_maxTime = 10.0f; function OnGUI() { if(GUI.Button(new Rect(30,350,80,80)," ",tombol1)){ m_timer = m_maxTime ; } if(m_timer >= 0.0f){ m_timer -= Time.deltaTime; // Do stuff... } }
ok, i've try to do yours, but when i do the timer just not decreasing, it stop at some value, and repeat.. also,use popUP because i must get rid of the other button, so player will not able to press the other buttons.. and i need to GET the value of time when the player press a, b, or c