- Home /
Simple Timer
Hi there, I'm quite new to this and it is probably really easy to figure out, and it's a tiny thing but it's bugging me. I have a simple timer for my game, counting down from 1:15 to 0. When the counter reaches a number range of 0 - 9, meaning like '1 : 09' or '0 : 09' it will simply display it as '1 : 9' or '0 : 9', and I would like it as '1 :09' or '0:09'.
Here's my code, just going chuck in the whole thing, the timer code is in bold.
//WinZone And Timer Script
//Time Limit for this level var totalTime = 75; var DeathLayer : GameObject;
//Some Variables we need to declare var timeLeft = totalTime; private var Display; private var LoadingSequence: boolean = false; private var RunTimer: boolean = true; private var OutOfTime: boolean = false;
function Update() { //If we're not in the WinZone, run the timer if (RunTimer ==true) { var thisSecond; var lastSecond =0; timeLeft = totalTime - Time.timeSinceLevelLoad; if (timeLeft < 1) { OutOfTime =true; }
}
//constrain time limit minimum timeLeft = Mathf.Max (0, timeLeft);
//Run a function that converts the time layout from a float to Minutes and Seconds TimerLayout (timeLeft);
}
//Convert time to minutes and seconds function TimerLayout(time){ var Clock : int = time; var minutes : int = Clock / 60; var seconds : int = Clock % 60;
//Convert output of minutes and seconds to strings and add a ":" in between. Display = minutes.ToString () + " : "; Display = Display + seconds.ToString (); }
//if player enters the WinZone function OnTriggerEnter (other : Collider){ Destroy (DeathLayer); //Show the endlevel gui elements LoadingSequence = true; //stop the timer RunTimer = false; } var Style_1 : GUIStyle; var Style_2 : GUIStyle; var Style_3 : GUIStyle; var Style_4 : GUIStyle; var Style_5 : GUIStyle; var Button_1 : GUIStyle; var Button_2 : GUIStyle; var Button_3 : GUIStyle; var Button_4 : GUIStyle; function OnGUI () { //show timer
if (LoadingSequence ==false)
{
GUI.Box (Rect ((Screen.width - Screen.width + 10), (Screen.height - Screen.height + 10), 200, 40), "TIME = " + Display, Style_4);
if (timeLeft == 10)
{
Style_4 = Style_5;
}
}
//if player is in the WinZone, show differnt gui elements
if (LoadingSequence ==true && OutOfTime == false)
{
GUI.Box (Rect ((Screen.width - 360) / 2, (Screen.height - -300) / 2, 370, 120), "CONGRATS! LEVEL ACCOMPLISHED!", Style_1);
GUI.Label (Rect ((Screen.width - 125) / 2, (Screen.height - -390) / 2, 140, 100), "TIME = " + Display, Style_2);
if (GUI.Button (Rect ((Screen.width - 320) / 2, (Screen.height - -460) / 2, 200, 28), "RETURN TO LEVEL ROOM", Button_1))
{
Application.LoadLevel("Level_Room");
}
if (GUI.Button (Rect ((Screen.width - -115) / 2, (Screen.height - -460) / 2, 111, 28), "NEXT LEVEL", Button_2))
{
Application.LoadLevel("Level_2");
}
}
if (OutOfTime == true)
{
GUI.Box (Rect ((Screen.width - 240) / 2, (Screen.height - -300) / 2, 240, 100), "OUT OF TIME!", Style_3);
Screen.lockCursor = false;
if (GUI.Button (Rect ((Screen.width - 80) / 2, (Screen.height - -370) / 2, 90, 20), "TRY AGAIN?", Button_3))
{
Application.LoadLevel(Application.loadedLevel);
}
if (GUI.Button (Rect ((Screen.width - 40) / 2, (Screen.height - -440) / 2, 50, 20), "QUIT?", Button_4))
{
Application.LoadLevel("Level_Room");
}
}
}
And yes, I know, it's real sloppy, just getting the basics there then going to tidy it up. Thank's in advance for the help.
Answer by TRAUMA · Mar 31, 2012 at 12:03 PM
And half my code hasn't come out as a 'sample'. Sorry guys, it's pretty unreadable, any help from what can be made from it would be appreciated, thank's.
Answer by dvidunis · Mar 31, 2012 at 09:49 AM
is this simple? simple is this:
var timer:float = 0;
function Start(){ while(true){ yield WaitForSeconds(0.1); timer += 0.1; }}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Disply ad not calling in Device 0 Answers
Script help please 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Trying to Create a Powerup that lasts a certain amount of time 1 Answer