• 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
11
Question by CarlLawl · Feb 01, 2011 at 06:21 PM · timersecondsminutes

making a timer (00:00) minutes and seconds

Ok, so Im trying to make a game timer that increments every second and displays it in the format of MM:SS

Heres what I have

 public static float timer;
 public static bool timeStarted = false;
 
 void Update ()
  {
     if (timeStarted == true) 
    {
         timer += Time.deltaTime;
     }       
 }
 
 void OnGUI()
  {
     float minutes = Mathf.Floor(timer / 60);
     float seconds = timer%60;
 
     GUI.Label(new Rect(10,10,250,100), minutes + ":" + Mathf.RoundToInt(seconds));
 }


but it outputs M:S (if the seconds is over 10 then obviously the seconds will be double digit) would anyone be able to explain where im going wrong?

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

10 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by sacredgeometry · Aug 14, 2019 at 07:20 AM

Please ignore my morning sniffles. but heres a video explaining (if it needs it.)

Video

Wow there are some over engineered solutions here.

You could just store the DateTime you triggered the timer and then calculate the timespan by subtracting it from the current time.

     DateTime _timer;
 
     void Update()
     {
         // Your triggering mechanism just sets the _timer to DateTime.Now Somewhere
         if(Input.GetKeyDown(KeyCode.Space)) _timer = DateTime.Now;

         if(_timer != default(DateTime))
         {
             Debug.Log(DateTime.Now.Subtract(_timer).ToString(@"mm\:ss"));
         }
     }


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 CaptMuchacho · Nov 24, 2015 at 05:00 PM

This is what i made for my countdown timer using UI Text.

 public Text TimerText;
 public float Minutes;
 public float Seconds;

 void Start()
 {
     StartCoroutine(Wait());
 }

 void Update()
 {
     if(Seconds < 10)
     {
     TimerText.text = (Minutes + ":0" + Seconds);
     }

     if(Seconds > 9)
     {
         TimerText.text = (Minutes + ":" + Seconds);
     }
 
 }


 public void CountDown()
 {
     if(Seconds <= 0)
     {
         MinusMinute();
         Seconds = 60;
     }

     if(Minutes >= 0)
     {
         MinusSeconds();
     }

     if(Minutes <= 0 && Seconds <= 0)
     {
         Debug.Log("Time Up");
         StopTimer();
     }
     else
     {
         Start ();
     }
     
 }

 public void MinusMinute()
 {
     Minutes -= 1;
 }

 public void MinusSeconds()
 {
     Seconds -= 1;
 }

 public IEnumerator Wait()
 {
     yield return new WaitForSeconds(1);
     CountDown();
 }

 public void StopTimer()
 {
     Seconds = 0;
     Minutes = 0;
 }
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 komal2992 · Jan 17, 2018 at 06:57 AM

Guys it's very simple for batter result try this one ## CountDown In hours, Minutes and seconds ##

countDown -= Time.deltaTime;

countDownText.text = (((Mathf.Floor(countDown / 3600f)) % 60).ToString("00")) + ":" + (((Mathf.Floor(countDown / 60f)) % 60).ToString("00")) + ":" + (Mathf.Floor(countDown % 60f).ToString("00"));

@CarlLawl

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 talticgames · Jun 05, 2019 at 02:14 PM

Too many complex answers here! Because from what's your question i can see that you are an amateur i suggest you using this simple code:

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine.UI;
     using UnityEngine;
     
     public float seconds;
     public float minutes;
     public Text timeCountText;
     
     
     void Start(){
     
     }
     
     void Update(){
     timer += Time.deltaTime;
     if (seconds > 59){
     minutes += 1;
     seconds = 0;
     }
     
     timeCountText.text = minutes.ToString("00") + " : " + seconds.ToString("00");
     
     }
 }









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 sasharomanov9529 · Jun 22, 2020 at 02:53 PM

 private Text m_gameTime;
 
 void Update()
 {
     float gameTimer += Time.deltaTime * 600;
 
     int seconds = (int)(gameTimer % 60);
     int minutes = (int)(gameTimer / 60);
     int hours = (int)(gameTimer / 3600);
 
     string timerString = string.Format("{0:0}:{1:00}:{2:00}", hours, minutes, seconds);
 
     m_gameTime.text = timerString;
 }


@CarlLawl

, private Text m_gameTime;

 void Update()
 {
     float gameTimer += Time.deltaTime * 600;
 
     int seconds = (int)(gameTimer % 60);
     int minutes = (int)(gameTimer / 60);
     int hours = (int)(gameTimer / 3600);
 
     string timerString = string.Format("{0:0}:{1:00}:{2:00}", hours, minutes, seconds);
 
     m_gameTime.text = timerString;
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

27 People are following this question.

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

Related Questions

How to stop a Countdown Timer? 1 Answer

Making a timer 1 Answer

Countdown Timer Help About putting 0's 1 Answer

Turn seconds into minutes and seconds MM:SS 1 Answer

I'm trying to get and store max elapsed time in min:sec format. Also, the best time for Highscore. Not working! 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