InvokeRepeating Bug or misuse?

Hello all,

I have been trying for quite a while to track down a very annoying problem in my script and I finally found out that the troublemaker is apparently InvokeRepeating. I made a simple script to demonstrate the problem I am facing.

var Test : int = 0;
var Rate : float = 0.2;

function Update () 
{
	if(Input.GetKeyDown("mouse 0"))
		InvokeRepeating("TestCounter", 0, Rate);
	if(Input.GetKeyUp("mouse 0"))
		CancelInvoke();
}

function TestCounter()
{
	Test++;
}

This is a script that should advance the counter when left mouse button is pressed at a rate specified by the Rate variable. It does that at all times except on the first frame. When it starts counting, depending on the rate of repeat, it advances 6 or more times. Results I got are that at a rate of 0.2 if I just super quick press and release mouse button it counts 6… if rate is 0.5 (to absolutely make sure that button is pressed for a shorter duration of time than the rate is) it still counts 3-4 for just one click… all consequent counting is correct, but the first click is never 1…

Can someone tell me what is going on and how I can overcome this problem? I assume that I am doing something wrong, and I tried really hard to figure it out, but I just can’t, since I’m a newbie :slight_smile:

Thanks!

Using 0 for the first parameter in InvokeRepeating is problematic, so it’s better to use .001.

–Eric

InvokeRepeating in general is problematic here, its a “click response” and the function there should normally only run once so you want to use Invoke, not InvokeRepeating and check if the button is still down and then yield into the next frame to check again.

A time based checking makes totally no sense as the button up / button down happens at framerate, not faster

I don’t see any issues with the general usage of InvokeRepeating here. It works fine, as long as you use .001 instead of 0.

–Eric

That part is true, but bases on the fact that Invokes are currently force synced in Unitys Mono

Thanks guys, works perfect now! :slight_smile: