Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by simpleone · May 14, 2011 at 11:10 AM · timercounter

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
10

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;
         }    
     }


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Happy-Zomby · Apr 11, 2018 at 03:59 PM 0
Share

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.

avatar image iwinxp · Sep 09, 2020 at 07:08 AM 0
Share

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;
         }
     }    
 }
avatar image
5

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chuckler · Jun 13, 2012 at 09:54 PM 0
Share

whoa, that works awesome..! so you can attach scripts to GUI texts, huh? damn good!

avatar image
0

Answer by Dreamer · May 14, 2011 at 11:21 AM

var timer:var=0;

function Update(){ timer+=Time.deltaTime;

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image simpleone · May 14, 2011 at 11:29 AM 0
Share

Yeah, but I wanna show it in the game - in a gui

avatar image
0

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();

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by MacMac098 · Jun 05, 2011 at 01:52 PM

var Timer : float;

function Update () { Timer += Time.deltaTime;

guiText.text = Timer.ToString(); }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can anyone help ?? Timer and gui 0 Answers

Stop watch for 100 meter dash 1 Answer

C# countdown timer 9 Answers

Timer to call Instantiate 1 Answer

reset timer using a GUI button 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges