- Home /
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
Answer by Kacer · Jun 20, 2011 at 10:16 AM
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.
Your answer
Follow this Question
Related Questions
C# countdown timer 9 Answers
How to add a counter on screen! 2 Answers
How to stop a countdown from counting down 1 Answer
Can anyone help ?? Timer and gui 0 Answers
Countdown Through a Label? 1 Answer