- Home /
 
 
               Question by 
               Lukamyte · Jun 03, 2014 at 11:35 PM · 
                variabletimesubtracting  
              
 
              Time variables not interacting
Hi. I have a script and it has a control to keep the entire level from exploding. Yes, it literally explodes. It is basically this:
 public static var time : float;
 public static var timeLess : float;
 
 function Start () {
     time = 0;
 }
 
 function Update () {
     time = Time.time - timeLess;
     
     if(timeLess < time){
         timeLess = time;
     }
 }
 
               But the thing is time does not reset, and it needs to be public and static so that other objects can reference it. Thanks in advance.
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by rutter · Jun 03, 2014 at 11:45 PM
You set time to 0 in Start, but you completely overwrite that information in every Update cycle.
You can either keep a "base" time:
 function Start() {
     baseTime = Time.time;
 }
 
 function Update() {
     time = Time.time - baseTime;
 }
 
               Or use timeSinceLevelLoad:
 function Update() {
     time = Time.timeSinceLevelLoad;
 }
 
               Or increment by deltaTime once per frame:
 function Start() {
     time = 0;
 }
 
 function Update() {
     time += Time.deltaTime;
 }
 
              Your answer
 
             Follow this Question
Related Questions
Subtracting variable amount over time? 1 Answer
time parsing making Game unable to run on editor 0 Answers
Played time? 2 Answers
How often is the update function called ? 1 Answer
Random time interval 1 Answer