- Home /
 
               Question by 
               Davidflynn · Nov 28, 2011 at 09:36 PM · 
                timetimertimer-script  
              
 
              Timer ends game
I have this code but once it reaches 0 it just starts to go negative, what I want is if time ends(0) it loads my end game screen.
Any help would be appresheated thanks.
 private var startTime;
 private var restSeconds : int;
 private var roundedRestSeconds : int;
 private var displaySeconds : int;
 private var displayMinutes : int;
 
 var countDownSeconds : int;
 
 function Awake() {
     startTime = Time.time;
 }
 
 function OnGUI () {
     //make sure that your time is based on when this script was first called
     //instead of when your game started
     var guiTime = Time.time - startTime;
 
     restSeconds = countDownSeconds - (guiTime);
 
     //display messages or whatever here -->do stuff based on your timer
     if (restSeconds == 60) {
         print ("One Minute Left");
     }
     if (restSeconds == 0) {
         print ("Time is Over");
         //do stuff here
     }
 
     //display the timer
     roundedRestSeconds = Mathf.CeilToInt(restSeconds);
     displaySeconds = roundedRestSeconds % 60;
     displayMinutes = roundedRestSeconds / 60; 
 
     text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds); 
     GUI.Label (Rect (400, 25, 100, 30), text);
     }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by burnumd · Nov 28, 2011 at 10:10 PM
OnGUI is called multiple times per frame and a new frame is rendered multiple times per second, so it's very unlikely that restSeconds will ever be exactly equal to 60 or 0. What you ought to do is check whether restSeconds
 //your existing vars
 private var oneMinuteWarningFired : bool = false;
 //Your other code up to OnGUI
 function OnGUI ()
 {
     // Your onGUI time setting code.
     if (restSeconds <= 60 && !oneMinuteWarningFired)
     {
         print ("One minute left");
         oneMinuteWarningFired = true;
     }
     if (restSeconds <= 0)
     {
         print ("Time is Over");
     }
 }
 
 // and so on
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                