x


Count Down Timer.

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 :/

more ▼

asked Feb 22 '10 at 07:34 AM

Will gravatar image

Will
408 8 10 19

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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);
}
more ▼

answered Feb 22 '10 at 08:35 AM

Sebas gravatar image

Sebas
4k 12 18 45

I changed the code so it doesn't have rounding problems anymore. It should now display correctly; also when changing the minutes.

Feb 22 '10 at 10:46 AM runevision ♦♦

Thanks for that. Much appreciated :)

Feb 22 '10 at 11:00 AM Sebas

also you can move variable declarations from OnGUI to the top to prevent unneeded allocations and deallocations

Feb 22 '10 at 11:33 AM Ashkan_gc

Thank you so much, this has really helped :)

Feb 22 '10 at 03:46 PM Will

Thanks mate, I was looking for this.

Apr 15 '10 at 03:00 PM Nom
(comments are locked)
10|3000 characters needed characters left

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"):
   }
}
more ▼

answered Feb 22 '10 at 11:29 AM

Ashkan_gc gravatar image

Ashkan_gc
9.1k 33 56 117

(comments are locked)
10|3000 characters needed characters left

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);
    }}`
more ▼

answered Dec 10 '10 at 06:28 PM

Samir 1 gravatar image

Samir 1
147 15 15 26

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x349

asked: Feb 22 '10 at 07:34 AM

Seen: 35761 times

Last Updated: Oct 16 '12 at 05:10 PM