- Home /
 
Time Counter - up
Hey, I've been searching around for a while now and can only find countdown timers when I want a timer that counts up when the scene is loaded... anyone know a answered question or example?
Answer by RonnyKibet · Jul 19, 2016 at 08:03 PM
Here is my little counter script that will count up like a timer clock.
     public Text timerText;
     private float secondsCount;
     private int minuteCount;
     private int hourCount;
     void Update(){
         UpdateTimerUI();
     }
 //call this on update
     public void UpdateTimerUI(){
         //set timer UI
         secondsCount += Time.deltaTime;
         timerText.text = hourCount +"h:"+ minuteCount +"m:"+(int)secondsCount + "s";
         if(secondsCount >= 60){
             minuteCount++;
             secondsCount = 0;
         }else if(minuteCount >= 60){
             hourCount++;
             minuteCount = 0;
         }    
     }
 
              Hi, if you want to display seconds or $$anonymous$$utes under 10 with a 0 - meaning 2 digits - you can change the line of Ronny$$anonymous$$ibet to:
 timerText.text = hourCount + "h:" + $$anonymous$$uteCount.ToString("00") + "m:" + ((int)secondsCount).ToString("00") + "s";
 
                  thanks for the counter.
There is a $$anonymous$$or flaw in this piece of code. When resetting the seconds or $$anonymous$$utes, you should use the % operator instead.
 secondsCount = 0;
 // should be replaced by: 
 secondsCount %= 60;
 
                  likewise
 $$anonymous$$uteCount = 0;
 // should be replaced by: 
 $$anonymous$$uteCount %= 60;
 
                  You'd also want to split this up into two nested if statements instead of an if-else statement This way, you don't throw away the overflow, and you don't waste computing resources.
The result is then:
 public void UpdateTimerUI(){
     //set timer UI
     secondsCount += Time.deltaTime;
     timerText.text = hourCount +"h:"+ $$anonymous$$uteCount +"m:"+(int)secondsCount + "s";
     if(secondsCount >= 60){
         $$anonymous$$uteCount++;
         secondsCount %= 60;
         if($$anonymous$$uteCount >= 60){
             hourCount++;
             $$anonymous$$uteCount %= 60;
         }
     }    
 }
                 Answer by FLASHDENMARK · May 14, 2011 at 11:27 AM
var Timer = 0.0;
 
               function Update () { Timer += Time.deltaTime; //Time.deltaTime will increase the value with 1 every second. } 
 
               
               If you want to show it on a GUI then you can use this:
first create a guiText then place this script on it:
var Timer = 0.0;
 
               function Update () { Timer += Time.deltaTime;
 
               guiText.text = "" + Timer; } 
 
               
               You can take a look at this.
whoa, that works awesome..! so you can attach scripts to GUI texts, huh? damn good!
Answer by Dreamer · May 14, 2011 at 11:21 AM
var timer:var=0;
 
               function Update(){ timer+=Time.deltaTime;
 
               } 
 
              Answer by nikocrow · Dec 11, 2012 at 05:33 AM
Put this in an GUI Text
var textTime : String; function Start () {
}
function Update () {
 var Crono = Time.time;
 
               var minutos : int = Crono / 60; var segundos: int = Crono % 60; var milesimas: int = (Crono * 100)% 100; textTime = String.Format ("{0:00}:{1:00}:{2:00}", minutos, segundos, milesimas); GetComponent(GUIText).text = textTime.ToString();
}
Answer by MacMac098 · Jun 05, 2011 at 01:52 PM
var Timer : float;
function Update () { Timer += Time.deltaTime;
guiText.text = Timer.ToString(); }
Your answer
 
             Follow this Question
Related Questions
How to realize accurate time counter (millisecond precision) 3 Answers
GUI Help, timer and counter 1 Answer
player on fire timer 2 Answers
How to add a counter on screen! 2 Answers