Author Topic: Controlling NGUI lables from one script.  (Read 14749 times)

greigy

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 4
    • View Profile
Controlling NGUI lables from one script.
« on: February 19, 2015, 01:32:19 AM »
The following is a script that I am in the process of creating for the purposes of a count down timer. For the sake of alignment and effects, I have created three labels as individual parts, but am seemingly unable to update the seconds. By disabling (commenting out) the minutes, the seconds are updated. - I have got rid of the milsec calcs to make sure they aren't influencing anything.

So what I need is a way to make all three update - or my mistake pointed out...


  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4.  
  5. public class EHBoardTimer : MonoBehaviour {
  6.  
  7.         public UILabel theMilliSeccondsCounter;
  8.         public UILabel theSeccondsCounter;
  9.         public UILabel theMinutesCounter;
  10.         public float levelTime = 150.0f;
  11.  
  12.         private float startTime = 0.0f;
  13.         private float theDeltaTime = 0.0f;
  14.  
  15.         private float theMinutes = 0.0f;
  16.         private float theSecconds = 0.0f;
  17.         private float theMillisecconds = 0.0f;
  18.  
  19.         // Use this for initialization
  20.         void Start ()
  21.         {
  22.                 startTime = Time.time;
  23.        
  24.         }
  25.        
  26.         // Update is called once per frame
  27.         void Update ()
  28.         {
  29.                 theDeltaTime = Time.time - startTime;
  30.                 theDeltaTime = levelTime - theDeltaTime;
  31.  
  32.                 // get milliseconds to two dp by removing truncated value
  33.                 //theDeltaTime = (float)Math.Round((double)theDeltaTime, 2);
  34.                 //theMillisecconds = theDeltaTime - Mathf.Round(theDeltaTime);
  35.                 theDeltaTime = Mathf.Round(theDeltaTime);
  36.  
  37.                 // get secconds
  38.                 theSecconds = theDeltaTime %60;
  39.                 Debug.Log(theSecconds);
  40.  
  41.                 // get minutes
  42.                 theMinutes = theDeltaTime / 60;
  43.                 theMinutes = Mathf.Round(theMinutes);
  44.  
  45.                 // write to screen
  46.                 theMilliSeccondsCounter.text = theMillisecconds.ToString();
  47.                 theMinutesCounter.text = theSecconds.ToString();
  48.                 theMinutesCounter.text = theMinutes.ToString();
  49.         }
  50. }
  51.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Controlling NGUI lables from one script.
« Reply #1 on: February 19, 2015, 02:04:27 PM »
  1.         static public string FormatTime (int sec)
  2.         {
  3.                 int min = sec / 60;
  4.                 sec = sec - (min * 60);
  5.                 return min + ":" + (sec < 10 ? "0" + sec : sec.ToString());
  6.         }

greigy

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Controlling NGUI lables from one script.
« Reply #2 on: February 19, 2015, 05:43:07 PM »
Hi,

what you have posted would be the next step, but...

it already calculates the seconds and mins. If you run the code and check the console, you will see the seconds ticking down.

What it is NOT DOING is updating the NGUI labels for seconds. Sorry if I wasn't clear before.

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Controlling NGUI lables from one script.
« Reply #3 on: February 20, 2015, 01:52:45 AM »
  1. theMinutesCounter.text = theSecconds.ToString();
  2. theMinutesCounter.text = theMinutes.ToString();
  3.  

your seconds label is wrong.

ps:  invokerepeating for timer updates is another possible alternative to Update()

greigy

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Controlling NGUI lables from one script.
« Reply #4 on: February 20, 2015, 06:28:09 PM »
My fault for looking at the same code for too long!

- thanks