Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Mickman on March 15, 2013, 04:39:10 AM

Title: A simple Count down timer ? [SOLVED]
Post by: Mickman on March 15, 2013, 04:39:10 AM
Hi... I'm new to NGUI & am wondering how to get  countdown timer to display on screen.

I have the countdown code operating in Unity4 Pro.  with the standard Unity Gui.  but now I am learning NGUI.

Perhaps someone can direct me to either a tutorial or simple help me out. 

   

Title: Re: A simple Count down timer ?
Post by: Nicki on March 15, 2013, 07:43:05 AM
Make a label and change the label.text the way you would change it in the unity label.

  1. public UILabel myLabel;
  2.  
  3. void Update()
  4. {
  5.   //given time logic
  6. myLabel.text = "Some time thing put out as a string in here";
  7. }
Title: Re: A simple Count down timer ?
Post by: Mickman on March 16, 2013, 03:08:17 AM
So I get no errors but the label is not counting down...   
There are no numbers populating the label area..
                          ( Are there any numeric NGUI examples... ?? )

Well here's my code so far.... 


  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CountDown : MonoBehaviour {
  5.  
  6.  
  7. public UILabel myLabel;
  8.        
  9. public float myTimer = 20.0f;
  10.  
  11. void Update () {       
  12.                 if(myTimer>0) {
  13.     myTimer -= Time.deltaTime;
  14.         int minutes = Mathf.FloorToInt(myTimer / 60F);
  15.     int seconds = Mathf.FloorToInt(myTimer - minutes * 60);
  16.  
  17.     string niceTime = string.Format("{0:0}:{1:00}", minutes, seconds);
  18.  
  19.     myLabel.text= niceTime;
  20.     //Application.LoadLevel(1);
  21.         }      
  22.        
  23.   }
  24. }
  25.  
Title: Re: A simple Count down timer ?
Post by: Nicki on March 16, 2013, 07:44:09 AM
Have you set the reference in the inspector? The code looks right otherwise.
Title: Re: A simple Count down timer ?
Post by: Mickman on March 16, 2013, 08:37:17 AM
HA--HAA !!!!  IT'S ALIVE !!!   ;D

Thanks so much that was the culprit... I'd forgotten to drag the counter onto the "myLabel" variable in the inspector.

 Ok.. that helps so much.. something so incredibly small yet incredibly important to get up and moving.

 I really appreciate the quick reply... I was starting to dwell on my code. 

 Mick
Title: Re: A simple Count down timer ? [SOLVED]
Post by: mremus on April 04, 2013, 08:59:04 AM
I am new to Unity and scripting so this is a real noob question.  Do you add the Countdown script as an component to an empty object or an NGUI label in order to get the timer to show up in when running the game?  Thanks ahead of time.
Title: Re: A simple Count down timer ? [SOLVED]
Post by: llamapixel on May 31, 2014, 08:19:23 PM
Hi guys,

Thanks for posting this.
I managed to get a simple timer to work with this advice.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class countDownTimer : MonoBehaviour {
  5.  
  6.         public UILabel NguiTimeLabel;
  7.         public float myTimer = 180.0f;
  8.         public int Triggerflag = 1;
  9.  
  10.         void Update () {       
  11.  
  12.  
  13.                 if(myTimer > 1.0) {
  14.                         myTimer -= Time.deltaTime;
  15.                         int minutes = Mathf.FloorToInt(myTimer / 60F);
  16.                         int seconds = Mathf.FloorToInt(myTimer - minutes * 60);
  17.                         string niceTime = string.Format("{0:0}:{1:00}", minutes, seconds);
  18.                         NguiTimeLabel.text= niceTime;
  19.                 }      
  20.                 if(myTimer < 1.0) {
  21.  
  22.                         if (Triggerflag == 1){
  23.                                 Debug.Log("Game Complete");
  24.                                 Triggerflag = 0;
  25.                                 NguiTimeLabel.text = "time's up";
  26.                         }
  27.  
  28.  
  29.                 }
  30.                
  31.         }
  32. }
  33.