Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //the total time for the timer to run (Since it's a public variable, the timer can be changed in unity via the inspector).
- public var totalTime:int = 180;
- //the curent time of the timer
- private var currentTime:int = 180;
- //the GUIText Component the time will be written to
- private var timerText:GUIText;
- //Whether the timer is running or not
- private var running:boolean = true;
- public var numberOfSeconds:int = 0;
- function Start()
- {
- currentTime = totalTime;
- timerText.enabled = true;
- //retrieve the GUIText Component
- timerText = gameObject.GetComponent(GUIText);
- //assign the totalTime to the textfield of the GUIText
- timerText.text = totalTime.ToString();
- //call TimerTick to start the timer ticking
- TimerTick();
- }
- /**
- Function to tick the timer by 1 second and update the text
- */
- function TimerTick()
- {
- // use a while loop to create a controlled infinite loop
- while (running)
- {
- // cause a delay of 1 second within the script
- yield WaitForSeconds (1);
- //increments the time by 1 second
- numberOfSeconds++;
- // reduce the current time
- currentTime--;
- // assign the currentTime to the textfield of the GUIText
- timerText.text = currentTime.ToString();
- // if timer runs out (i.e. currentTime == 0) stop the loop
- if (currentTime ==0){
- // Application.LoadLevel("menu");
- timerText.text = ("GAME OVER");
- break;
- }
- }
- }
- function addTime(seconds:int)
- {
- currentTime+=seconds;
- }
Add Comment
Please, Sign In to add comment