Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
0
Question by saswilson · Jul 31, 2013 at 04:18 PM · timercountdown

Countdown timer in minutes:seconds:milliseconds

Hi, Im trying to make a countdown timer in c# that displays a GUIText in minutes:seconds:milliseconds.

I've looked and used the timers that have been suggested on here but I cant get one to work using milliseconds. I need it to start after a 3,2,1 countdown (that's another script) and stop at 00:00:000 Thanks for any help.

Comment
Add comment · Show 3
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 nixcs2512 · Jul 31, 2013 at 05:12 PM 0
Share

Actually the time in-game in unity cant give you right amount at all cases, it based on render time ( you can check this time by using Time.deltaTime ) and each frame has its own render time depends on many things. So i think you need a "fake" thing here. Say if the timer

avatar image nixcs2512 · Jul 31, 2013 at 05:16 PM 0
Share

One more thing, people says yield works well but i dont know if it work perfectly with your case. I dont use it just because it confused me when i started using Unity, and i could handle the time better with Time.deltaTime. But anyway, try it :)

avatar image saswilson · Jul 31, 2013 at 06:19 PM 0
Share

cheers thanks for your help

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by jacobschellenberg · Jul 31, 2013 at 05:30 PM

This base timer I wrote really quick should give you a good start.

     using UnityEngine;
     using System.Collections;
     
     public class DisplayTimer: MonoBehaviour {
         
         public TextMesh timer;
         float minutes = 5;
         float seconds = 0;
         float miliseconds = 0;
         
         void Update(){
             
             if(miliseconds <= 0){
                 if(seconds <= 0){
                     minutes--;
                     seconds = 59;
                 }
                 else if(seconds >= 0){
                     seconds--;
                 }
                 
                 miliseconds = 100;
             }
             
             miliseconds -= Time.deltaTime * 100;
             
             //Debug.Log(string.Format("{0}:{1}:{2}", minutes, seconds, (int)miliseconds));
             timer.text = string.Format("{0}:{1}:{2}", minutes, seconds, (int)miliseconds);
         }
     }


Hope this helps.

Comment
Add comment · Show 4 · 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 saswilson · Jul 31, 2013 at 08:55 PM 0
Share

Thanks, I'll give this a go later. I appreciate your help ;-)

avatar image _13PhysX · Apr 20, 2016 at 09:46 AM 0
Share

Thanks It really helpful!!!!!!!!!!!!

avatar image SupermikeAdventures · Feb 15, 2017 at 03:50 PM 0
Share

Thanks mate! This was very helpful. You're the best! :)

avatar image mentos120 · Dec 10, 2021 at 08:40 PM 0
Share

Wow... 8 years later and I am thankful for this lol, Thanks alot!

avatar image
1

Answer by HansMcCode · Jul 31, 2013 at 06:17 PM

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class CountdownTimer : MonoBehaviour
 {
     public float m_startingMinutes = 3.0f; //in seconds
     public bool m_startTimer = false;
     public GUIText m_timerLabel; //drag GUIText in the scene here
 
     private float m_miliseconds;
     private float m_seconds;
     private float m_mins;
     private float m_totalmiliseconds;
 
     //-----------------------------------------------------------------------------------
     //Note: If you had a way to take in a username or initials you could 
     //do something along the lines of the below code instead of just using 
     //a list
     //
     //private Dictionary<string, int> m_scoreDictionary = new Dictionary<string, int>();
     //
     //With a dictionary you could link a name to a score value, just a thought :P
     //For now this script just uses the list set up below.
     //-----------------------------------------------------------------------------------
     private List<int> m_scores = new List<int>();
 
     // Use this for initialization
     void Start()
     {
         this.Init(m_startingMinutes);
     }
 
     // Update is called once per frame
     void Update()
     {
         if (m_startTimer && m_totalmiliseconds >= 0)
         {
             if (m_miliseconds <= 0)
             {
                 if (m_seconds <= 0)
                 {
                     m_mins--;
                     m_seconds = 59;
                 }
                 else
                 {
                     m_seconds--;
                 }
 
                 m_miliseconds = 99;
             }
 
             m_miliseconds -= Time.deltaTime * 100;
             m_totalmiliseconds -= Time.deltaTime * 100;
         }
         else if (m_totalmiliseconds <= 0)
         {
             m_miliseconds = 0.0f;
             m_seconds = 0.0f;
             m_mins = 0.0f;
         }
 
         if ((int)m_miliseconds > 9)
         {
             m_timerLabel.text = string.Format("{0}:{1}:{2}", m_mins, m_seconds, (int)m_miliseconds);
         }
         else
         {
             m_timerLabel.text = string.Format("{0}:{1}:0{2}", m_mins, m_seconds, (int)m_miliseconds);
         }
     }
 
     /// <summary>
     /// Public function to initialize the timer
     /// </summary>
     /// <param name="p_startingTime"></param>
     public void Init(float p_startingTime)
     {
         //On the note of PlayerPrefs, you may want to read them in here on the initialize      
         m_totalmiliseconds = p_startingTime * (60/*seconds*/) * (100/*miliseconds*/);
         m_mins = p_startingTime;
         m_startTimer = true;
     }
 
     /// <summary>
     /// Public function to pause the timer
     /// </summary>
     /// <param name="p_pause"></param>
     public void PauseTimer(bool p_pause)
     {
         m_startTimer = p_pause;
     }
 
     /// <summary>
     /// Store the score in a list
     /// </summary>
     public void StoreScore()
     {
         //Store the total milisceconds left in as the score so it we want to
         //use it later we have a number we can format back into MM:SS:mm
         this.m_scores.Add((int)m_totalmiliseconds);
 
         //Something to consider here is that these score will not be persistent 
         //between games, you may want to consider using the PlayerPrefs to store
         //a string that can be parsed out and fed back into the m_scores list.
         //link: http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html
         
     }
 }

Mines quite similar to the one above. Just posting mine because it gives you the ability to start and stop it and set the time publicly so you could hook it into other scripts quite easily. Just thought the additions might be welcome :)

Cheers and good luck,

Hans

Comment
Add comment · Show 3 · 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 HansMcCode · Jul 31, 2013 at 06:28 PM 0
Share

Sorry I just noticed that $$anonymous$$e didn't stop at 0:00:00 so I updated it to have that behavior. I also don't think it will handle any starting number below one $$anonymous$$ute, so you may want to test for that. :)

avatar image saswilson · Aug 01, 2013 at 05:20 PM 0
Share

this is perfect, thanks again :)

avatar image saswilson · Aug 04, 2013 at 04:42 PM 0
Share

In my OnGUI function, I'm was displaying m_timerLabel.text as the finished time. But how do I convert m_totalmiliseconds into $$anonymous$$: sec: mili? So inside my OnGUI, m_totalmiliseconds is used to show the $$anonymous$$: secs etc Thanks for any help!

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

22 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

Related Questions

How to reset Time.time ? 1 Answer

How to use the same script on different objects at the same time? 1 Answer

advanced countdown timer help please 0 Answers

Start timer on button press 1 Answer

Photon Multiplayer Countdown TImer 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