Author Topic: A simple Count down timer ? [SOLVED]  (Read 10780 times)

Mickman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
A simple Count down timer ? [SOLVED]
« 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. 

   

« Last Edit: March 16, 2013, 08:38:00 AM by Mickman »

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: A simple Count down timer ?
« Reply #1 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. }

Mickman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: A simple Count down timer ?
« Reply #2 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.  
« Last Edit: March 16, 2013, 05:28:54 AM by Mickman »

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: A simple Count down timer ?
« Reply #3 on: March 16, 2013, 07:44:09 AM »
Have you set the reference in the inspector? The code looks right otherwise.

Mickman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: A simple Count down timer ?
« Reply #4 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

mremus

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: A simple Count down timer ? [SOLVED]
« Reply #5 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.

llamapixel

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: A simple Count down timer ? [SOLVED]
« Reply #6 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.