x


countdown/countup timer

hey everyone i have a question can anyone share a script with me or two i need a countdown scrpit of when the time is over it says game over and the game ends and another of count up timer to show how long you have bin playing for in seconds minutes,hours,milliseconds playing because i cant script for my life

more ▼

asked Jun 20 '11 at 08:20 AM

exotic_gaming gravatar image

exotic_gaming
-34 5 6 7

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

1 answer: sort voted first

here's a bit of code that might help:

public float maxlifetime = 10.0f; //you can edit this in the inspector to change how long your life should be
private float timespent = 0.0f; //this is basically how long you have been alive for
private bool isdead = false; //boolean that shows if you're alive or not.

public void Update(){
    timespent += time.deltatime; //time.deltatime is the time between each frame, update is called once every frame.
    if(timespent > maxlifetime){//when timespent gets higher than maxlifetime you die.
        isdead = true;
    }
}

you can do a print on the "timespent" in the gui to see how long you've been alive for, or how long you have left, though im not sure on how you can make it show up as hours:minutes:seconds. This piece of code isnt exactly what you'd call optimal, but it might work :)

private float seconds = 0.0f;
private float minutes = 0.0f;
private float hours = 0.0f;

public void Update(){
    seconds += time.deltatime;
    if(seconds > 60){
        minutes += 1;
        seconds = 0;
    }
    if(minutes > 60{
        hours += 1;
        minutes = 0;
    }
    print("Hours: " + hours + " " + "Minutes: " + minutes + " " + "Seconds" + seconds);

}

again, there is probably some smarter way, but this one should work.

though, if you want to use unity you should learn some scripting, as you cant rely on the community to make all your scripts for you.

more ▼

answered Jun 20 '11 at 10:16 AM

Kacer gravatar image

Kacer
705 14 17 31

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

x347
x100
x62

asked: Jun 20 '11 at 08:20 AM

Seen: 1056 times

Last Updated: Jun 20 '11 at 10:16 AM