- Home /
Question by
RadhaUnity3d · Mar 05, 2014 at 11:43 AM ·
timer-script
How to create a Game Time in mm:ss format
Hello every -body....I want to create a Time Inside a game..which starts with 00:00...when player hit start button ...and it should display ..in GUI texture..or GUI.Label.... first second should be loaded after that minutes ....any help would be great appreciable....
Comment
Have you been through this link? : http://answers.unity3d.com/questions/45676/making-a-timer-0000-$$anonymous$$utes-and-seconds.html
Best Answer
Answer by SirCrazyNugget · Mar 05, 2014 at 12:23 PM
Standalone script:
#pragma strict
private var timerOn : boolean; //if the timer is running or paused
private var timerCount : float; //the actual timer
private var timerStart : float; //the point in time the timer was started
private var timerString : String; //the display string for the timer
private var timerRect : Rect = Rect(10, 10, 200, 100);
private var startRect : Rect = Rect(10, 210, 100, 40);
private var resetRect : Rect = Rect(110, 210, 100, 40);
function Update () {
if(timerOn){
timerCount = Time.time - timerStart;
}
var seconds = Mathf.Floor(timerCount % 60);
var minutes = Mathf.Floor(timerCount / 60);
timerString = String.Format("{0:00}:{1:00}", minutes, seconds);
}
function OnGUI(){
//if timer is counting
if(timerOn){
if(GUI.Button(startRect, "Stop")){
timerOn = false; //toggle timer
}
//if timer has stopped
}else{
if(GUI.Button(startRect, "Start")){
timerOn = true; //toggle timer
timerStart = Time.time - timerCount;
}
}
//reset the timer
if(GUI.Button(resetRect, "Reset")){
timerOn = false;
timerCount = 0;
}
GUI.Label(timerRect, timerString);
}
Enjoy!
Your answer
Follow this Question
Related Questions
How to set On Screen Message w/ timer 3 Answers
How do I create a mission timer? 2 Answers
Minutes and Seconds in a text 1 Answer
timer not ticking down 2 Answers
Changing texture after 3s,4s and 5s 2 Answers