- Home /
invoke much faster than update timer?
Hi,
I've just upgraded my code from using timers to invokes.
Whereas before i had something like this in update:
 cooldown = 0 initially
 
     if(cooldown >= 0){
        cooldown -= Time.deltaTime;
     }
     else{
        dostuff()
        cooldown = cooldowntime;
    }
 
 now i have this:
 initially i call Dostuff()
 
 function DoStuff(){
    stuff happens;
    if(stuff not finished){
      invoke("DoStuff",cooldowntime);
    }
 }
The invoke code runs WAYYYY faster than the timer even though cooldowntime is exactly the same. Im not sure what i messed up? Is invoke frame rate independent or something compared to update timer maybe?
Why not use InvokeRepeating? http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.InvokeRepeating.html
no im not changing timescale when testing this.
also, i don't use invoke repeating because sometimes i change my cooldowntime and i don't want to go through the hassle of canceling the invoke repeating and restarting it using a new cooldowntime, when i could just use the code i have and simply change the variable itself.
i created a timer that increments between invokes, and set the invoke to 2 second delay, and the timer perfectly sticks to 2 seconds. LOL im having a feeling that maybe i possibly had some timer bug in my older code that made it run SLOWER than required, but im not 100% on that.
Answer by reivajzen · Oct 13, 2017 at 09:48 AM
Hi @luniac:
Maybe you found the answer before but in case you didn't, it's better StartCoroutine. In the linke http://answers.unity3d.com/questions/477862/what-is-the-best-between-startcoroutine-or-invoker.html you can find how the expert answered it.
cheers,
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                