Bug in InvokeRepeating when time is set to 0?

So, if i set the starting time of the repeating function to 0, it seems as if the functions returns a bit to fast after the first iteration.

If I run this script in an empty scene (which basically says that the function Invoke should be called every 2 seconds starting immediately:

public class Invoker : MonoBehaviour {

    int count;

    float startTime;

	void Start () 
    {
        startTime = Time.realtimeSinceStartup;
        InvokeRepeating("Invoke", 0, 2);
	}

    private void Invoke()
    {
        Debug.Log("count = " + count++ + " Time elapsed = " + (Time.realtimeSinceStartup - startTime));
    }
	
}

I get the result:

Count = 0 Time Elapsed = 0
Count = 1 Time Elapsed = 1
Count = 2 Time Elapsed = 3
Count = 3 Time Elapsed = 5
Count = 4 Time Elapsed = 7

But if i change the InvokeRepeating call to

InvokeRepeating(“Invoke”, 0.001f, 2);

I get the result:

Count = 0 Time Elapsed = 0 (basically)
Count = 1 Time Elapsed = 2
Count = 2 Time Elapsed = 4
Count = 3 Time Elapsed = 6
Count = 4 Time Elapsed = 8

Am i thinking about this the wrong way or is this a bug?

1 Answer

1

Yes, InvokeRepeating has behaved that way forever. Use a very small number instead of 0.