Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Creating a time countdown on GUI

Discussion in 'Scripting' started by Rgalaxy, Jan 23, 2014.

  1. Rgalaxy

    Rgalaxy

    Joined:
    Sep 30, 2013
    Posts:
    8
    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):
    1. function OnGUI()
    here's my code

    Code (csharp):
    1. function OnGUI() {
    2.  
    3.         if(GUI.Button(new Rect(30,350,80,80)," ",tombol1)){
    4.             popUp = true;
    5.                    
    6.            
    7.             if(enemy_hp<=0){
    8.                 enemy_hp=0;
    9.                 MonsterIsDead();
    10.             }
    11.             if(player_hp<=0){
    12.                 player_hp = 0;
    13.                 PlayerIsDead();
    14.             }  
    15.         }
    16.         else if(GUI.Button(new Rect(90,280,80,80)," ",tombol2)){
    17.            
    18.         }
    19.        
    20.         else if(GUI.Button(new Rect(130,350,80,80)," ",tombol3)){
    21.        
    22.         }
    23.    
    24.     if(popUp){
    25.            
    26.             GUI.BeginGroup(new Rect(208,97,350,350),soal);
    27.             GUI.Label (new Rect(100,20,50,50),seconds.ToString());
    28.             GUI.Button(Rect(50,280,50,50),"",buttonA);
    29.             GUI.Button(Rect(150,280,50,50),"",buttonB);
    30.             GUI.Button(Rect(250,280,50,50),"",buttonC);
    31.  
    32.             GUI.EndGroup();
    33.            
    34.             InvokeRepeating("Countdown",1.0,1.0);
    35.     }
    36. }
    37. function Countdown(){
    38.     if (--seconds == 0) CancelInvoke ("Countdown");
    39.     seconds = parseInt(seconds.ToString());
    40.     if(Input.GetKeyDown("a")){
    41.         CancelInvoke("Countdown");
    42.     }
    43. }
    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 ?
     
  2. cowtrix

    cowtrix

    Joined:
    Oct 23, 2012
    Posts:
    322
    Why not just:

    Code (csharp):
    1.  
    2. static float m_timer  = 0.0f;
    3. static float m_maxTime = 10.0f;
    4. function OnGUI() {
    5.     if(GUI.Button(new Rect(30,350,80,80)," ",tombol1)){
    6.         m_timer = m_maxTime ;
    7.     }
    8.     if(m_timer >= 0.0f){
    9.         m_timer -= Time.deltaTime;
    10.         // Do stuff...
    11.     }
    12. }
    13.  
     
  3. Rgalaxy

    Rgalaxy

    Joined:
    Sep 30, 2013
    Posts:
    8
    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
    $Capture.PNG