restarting InvokeRepeating

I have this script that uses InvokeRepeating. I can cancel it using CancelInvoke. How do I restart it? I tried enabling/disabling the script instead of CancelInvoke but that didn’t work.

Just call InvokeRepeating again.

–Eric

Hi Eric,

Well, that’s what I’m trying to figure out. I was using the example in the docs as a template and they don’t really show how to call the invoke. It’s just ON.

Would that be with InvokeRepeating?

EDIT: that makes no sense : that’s what I’m using in the first place!

Here’s my code. It’s to make a timer. It’s not very elegant but it works. I didn’t want to use Time.time because I couldn’t find a way to reset it to 0 and save the data in the prefs.

var Timer2 : int;
var Cam : GameObject;

private var CubeRotFuncScript: CubeRotFunc;
CubeRotFuncScript = Cam.GetComponent(CubeRotFunc);

InvokeRepeating ("TimerNew", 0, 1);

function Update() {
	if (CubeRotFuncScript.ResetTimer ==1){
		 CancelInvoke("TimerNew");
 	} 
 }
 

function TimerNew () {
   
Timer2=Timer2+1;

     var hours : int = (Timer2 /60)/60;
     var minutes : int = (Timer2 / 60) % 60; 
     var seconds : int = Timer2 % 60; 
     
     
     if ((seconds<10)(minutes<10)(hours<10)){
     	    guiText.text = ("0" + hours +":0"+ minutes +":0"+seconds );
     	}
     	
      if ((seconds<10)(minutes>9)(hours<10)){
     	     guiText.text = ("0" + hours +":"+ minutes +":0"+seconds );
     	}    	
     	
       if ((seconds>9)(minutes>9)(hours<10)){
     	     guiText.text = ("0" + hours +":"+ minutes +":"+seconds );
     	} 
     	
       if ((seconds>9)(minutes<10)(hours<10)){
     	     guiText.text = ("0" + hours +":0"+ minutes +":"+seconds );
     	}	
   
       if ((seconds<10)(minutes<10)(hours>9)){
     	     guiText.text = (hours +":0"+ minutes +":0"+seconds );
     	}
     	
       if ((seconds<10)(minutes>9)(hours>9)){
     	     guiText.text = (hours +":"+ minutes +":0"+seconds );
     	}    	
     	
       if ((seconds>9)(minutes>9)(hours>9)){
     	     guiText.text = (hours +":"+ minutes +":"+seconds );
     	} 
     	
            if ((seconds>9)(minutes<10)(hours>9)){
     	     guiText.text = (hours +":0"+ minutes +":"+seconds );
     	}     	     	
}

Take out the Update function; that runs every frame. Just call CancelInvoke once when you need it, then InvokeRepeating when you want to start it up again.

Also, string formatting would get rid of all the if statements and reduce the size of that function to about 10% of what it is now. :slight_smile: Do some googling for “.net string formatting”.

–Eric

1 Like

i have the same problem here and can’t understand what should i do to solve it…
here’s my code :

var stopWalking : boolean = false;

InvokeRepeating ("showProps",2,1);
InvokeRepeating ("showReef",2,1);

function Update () {

	if(Input.GetAxis("Vertical")>0){
	stopWalking= false;
         }
     else{
	stopWalking = true;		
	}

        if(stopWalking==true){
	CancelInvoke();
        }

}

how could i restart the InvokeRepeating for the function when the variable stopWalking == false ??

There is an IsInvoking function that you can use to tell if a function is already being invoked. You could use something like:-

if (!IsInvoking("showProps"){
    InvokeRepeating ("showProps",2,1); 
}

…in the Update function without invoking over and over again.

what i would exactly do is to increse the calling time of the function , so i change the script like this :

static var repeatTime: float ;

function Update () {
	
var repeatTime = 100*(1/speed);

if (!IsInvoking("showProps")){
   InvokeRepeating ("showProps",2,repeatTime);
	}

}

but that didn’t change the time, it’s like the invoke function keep the first time and didn’t change it at all, is there any wrong on my script??

How are you reducing the speed value?

i called a function OnTriggerEnter when the player collides with the props, if the prop was the incorrect one, the speed of the props decrease ! as simple as it is :slight_smile:
here are the pseudo code for the function :

function OnTriggerEnter (other : Collider) {
if(CorrectCollider == other.name){
speed++;
	}
  else{
  speed--;
  }
}

so, any good ideas?