Author Topic: How to get the Countdown Timer as part of a Label  (Read 4395 times)

mremus

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
How to get the Countdown Timer as part of a Label
« on: April 04, 2013, 11:09:14 AM »
I pulled this Countdown Timer JavaScript from the Unity3D forum and made some minor tweaks to it. What I would like to do now is apply this script (modifying if I need to) to a NGUI Label so that the timer shows up in the Label and inherits the Font style and size attached to the Label.  As you can see at the end of the code, the code is already creating and placing a GUI.Box to display the timer in.  I need to get the countdown timer (textTime) to display in a Label.  Could someone give me some guidance on this.

#pragma strict

private var startTime : float;
private var restSeconds : int;
private var roundedRestSeconds : int;
private var displaySeconds : int;
private var displayMinutes : int;
 
var countDownSeconds : int;
var textTime : String; //added this member variable here so we can access it through other scripts
 
function Awake() {
    startTime = Time.time;
}
 
function OnGUI () {
    //make sure that your time is based on when this script was first called
    //instead of when your game started
    var guiTime : float = Time.time - startTime;
 
    restSeconds = countDownSeconds - (guiTime);
 
    //display messages or whatever here -->do stuff based on your timer
    if (restSeconds == 60) {
        print ("One Minute Left");
    }
    if (restSeconds == 0) {
        print ("Time is Over");
        //do stuff here
    }
 
    //display the timer
    roundedRestSeconds = Mathf.CeilToInt(restSeconds);
    displaySeconds = roundedRestSeconds % 60;
    displayMinutes = roundedRestSeconds / 60;
 
    textTime = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds);
    GUI.Box(Rect(10,10,100,30),textTime);
}
« Last Edit: April 04, 2013, 02:43:26 PM by mremus »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to get the Countdown Timer as part of a Label
« Reply #1 on: April 05, 2013, 09:34:54 AM »
Given that you have a UILabel label:

label.text = textTime;

mremus

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: How to get the Countdown Timer as part of a Label
« Reply #2 on: April 09, 2013, 04:40:00 PM »
Thanks!