- Home /
InvokeRepeating with a variable?
I was wondering if you could possibly use a variable to make InvokeRepeating repeat a random time? for example:
 var randomNumber : float =Random.Range(3, 10);
 
 function Start()
 {
 InvokeRepeating("Test", 1, randomNumber);
 }
 
 function Test()
 {
 print("Success!");
 }
Or perhaps I should do this:
 function Update()
 {
 
 yield WaitForSeconds(random);
 
 var randomNumber : float = Random.Range(3, 10);
 
 }
Answer by DynamicPrgm · Jan 03, 2013 at 10:21 AM
I figured out what I was trying to accomplish: random generation of a number once every X seconds, for decision making. Heres the code:
     var rand;
     
     function Start()
     {
     InvokeRepeating("RandomRanger", 0, 1.5);
     //sets function to repeat once every 1.5 seconds starting immediately
     }
     
     function RandomRanger()
     {
     var it = Random.Range(1, 5);
     rand = it;
     //makes random number
     }
     
     function Update(){
     print (rand);
     }
Now I realize what I was trying to accomplish was actually not what the question was...I was trying to do this for AI decisions. Now I'm much better at program$$anonymous$$g and am frowning upon my code in the question... I even spelled Yield wrong.
Answer by Eric5h5 · Nov 12, 2012 at 01:34 AM
If the goal is to have InvokeRepeating use a different length of time whenever it repeats, then no, that's not possible using InvokeRepeating. Instead you can use a coroutine. (But not like your second example, which can't work since Update can never be a coroutine. Just leave Update out of it and use a coroutine.)
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                