Ok, what I'm trying to do is make a count down timer that will start, at maybe 2 minutes, at the start of the game, and when It reaches 0:00 then some text will appear on the screen. I know how to do the text and how to start it, but I don't know how to make the actual timer, any Ideas?
P.S. I did check out the other two questions about counters and timers, but i couldn't do anything with that :/
I got a simple version of a countdown timer here. I'm sure there are other and more elegant versions out there (e.g. see this forum thread), but they are also a bit lengthy. See for yourself.
private var startTime;
private var restSeconds : int;
private var roundedRestSeconds : int;
private var displaySeconds : int;
private var displayMinutes : int;
var countDownSeconds : int;
function Awake() {
startTime = Time.time;
}
function OnGUI () {
//make sure that your time is based on when this script was first called
//instead of when your game started
var guiTime = Time.time - startTime;
restSeconds = countDownSeconds - (guiTime);
//display messages or whatever here -->do stuff based on your timer
if (restSeconds == 60) {
print ("One Minute Left");
}
if (restSeconds == 0) {
print ("Time is Over");
//do stuff here
}
//display the timer
roundedRestSeconds = Mathf.CeilToInt(restSeconds);
displaySeconds = roundedRestSeconds % 60;
displayMinutes = roundedRestSeconds / 60;
text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds);
GUI.Label (Rect (400, 25, 100, 30), text);
}
there are many ways to do this. for example another way would be
var showText=false;
function StopWatch (time:float)
{
yield WaitForSeconds (time);
showText=true;
}
function OnGUI ()
{
if (showText==true)
{
GUI.Label (Rect(100,100,300,150),"this text will be shown after the timer ends"):
}
}
so how did you sort the rounding problem??
nevermind i got it all you need to do is change some variables.
if anyone needs this in c# here you go :) you will need to change public CountDownSeconds in the inspector to any value of your choice ie, 10:00, you would write 600 in the inspector panel.
`using UnityEngine;
using System.Collections;
public class TimerScript : MonoBehaviour {
private float startTime;
private float restSeconds;
private int roundedRestSeconds;
private float displaySeconds;
private float displayMinutes;
public int CountDownSeconds=120;
private float Timeleft;
string timetext;
// Use this for initialization
void Start ()
{
startTime=Time.deltaTime;
}
void OnGUI()
{
Timeleft= Time.time-startTime;
restSeconds = CountDownSeconds-(Timeleft);
roundedRestSeconds=Mathf.CeilToInt(restSeconds);
displaySeconds = roundedRestSeconds % 60;
displayMinutes = (roundedRestSeconds / 60)%60;
timetext = (displayMinutes.ToString()+":");
if (displaySeconds > 9)
{
timetext = timetext + displaySeconds.ToString();
}
else
{
timetext = timetext + "0" + displaySeconds.ToString();
}
GUI.Label(new Rect(650.0f, 0.0f, 100.0f, 75.0f), timetext);
}}`
Here, very simple (I am using the code in a 3Dtext):
public var time : float;
function Update () {
time -= Time.deltaTime;
var minutes : int = time / 60;
var seconds : int = time % 60;
var fraction : int = (time * 100) % 100;
if (time>0)
//displaying in the 3Dtext
GetComponent(TextMesh).text = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
}