- Home /
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.
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
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 :)
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.
Thanks, I'll give this a go later. I appreciate your help ;-)
Thanks mate! This was very helpful. You're the best! :)
Wow... 8 years later and I am thankful for this lol, Thanks alot!
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
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. :)
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
Follow this Question
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