Guest User

Untitled

a guest
Jul 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   //the total time for the timer to run (Since it's a public variable, the timer can be changed in unity via the inspector).
  2.     public var totalTime:int = 180;
  3.    
  4.     //the curent time of the timer
  5.     private var currentTime:int = 180;
  6.    
  7.     //the GUIText Component the time will be written to
  8.     private var timerText:GUIText;
  9.    
  10.     //Whether the timer is running or not
  11.     private var running:boolean = true;
  12.    
  13.     public var numberOfSeconds:int = 0;
  14.    
  15.     function Start()
  16.     {
  17.     currentTime = totalTime;
  18.     timerText.enabled = true;
  19.        
  20.     //retrieve the GUIText Component
  21.     timerText = gameObject.GetComponent(GUIText);
  22.    
  23.     //assign the totalTime to the textfield of the GUIText
  24.     timerText.text = totalTime.ToString();
  25.    
  26.     //call TimerTick to start the timer ticking
  27.     TimerTick();
  28.     }
  29.     /**
  30.         Function to tick the timer by 1 second and update the text
  31.     */
  32.     function TimerTick()
  33.     {
  34.         // use a while loop to create a controlled infinite loop
  35.         while (running)
  36.         {
  37.             // cause a delay of 1 second within the script
  38.             yield WaitForSeconds (1);
  39.            
  40.             //increments the time by 1 second
  41.             numberOfSeconds++;
  42.              
  43.             // reduce the current time
  44.             currentTime--;
  45.              
  46.             // assign the currentTime to the textfield of the GUIText
  47.              timerText.text = currentTime.ToString();
  48.              
  49.             // if timer runs out (i.e. currentTime == 0) stop the loop
  50.              if (currentTime ==0){              
  51.             // Application.LoadLevel("menu");
  52.             timerText.text = ("GAME OVER");
  53.            
  54.             break;
  55.         }
  56.       }
  57.     }
  58.     function addTime(seconds:int)
  59.     {
  60.     currentTime+=seconds;
  61.     }
Add Comment
Please, Sign In to add comment