- Home /
 
 
               Question by 
               Flisijn · Mar 18, 2013 at 11:51 AM · 
                variabletimesubtracting  
              
 
              Subtracting variable amount over time?
How can I distract a variable over time? For example:
var amount : int = 10;
Every second (declared in a variable so I can change the speed) the amount is subtracted by 1. Can anyone help me?
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Fattie · Mar 18, 2013 at 11:55 AM
very simple, please learn about Invoke() and InvokeRepeating()
many beginner articles on this at unityGEMS.com
Here's a simple way to do it:
 var amount : int = 10;
 
               in your Start() routine...
 Invoke( "removeOneSecond", 1.0 );
 
               write a simple routine like this:
 function removeOneSecond()
   {
   if ( removeOneSecond < 0 ) return;
 
   removeOneSecond = removeOneSecond - 1;
   Debug.Log("removeOneSecond is now ... " + removeOneSecond);
 
   Invoke( "removeOneSecond", 1.0 );
   }
 
               OK? So it's that easy. Alternately you could use InvokeRepeating() and then when it is less than zero, CancelInvoke(). Cheers.
Your answer
 
             Follow this Question
Related Questions
Time variables not interacting 1 Answer
I can't get variable to decrease by one 1 Answer
Wait a specific time? 1 Answer
How to properly declare this variable 2 Answers