Call function every 0.1sec in Update

In the Update method if a condition is meet, i call a function.
But that function must be called every 0.1 seconds and not every frame.

How i can do that?

InvokeRepeating it’s not a solution for me.

Edit: I’ve come up with a solution, but i don’t know if it’s good to do this, and if there is a better solution please tell me:

private float sendRate = 0.1f;
private float nextTime = 0;
void Update(){
    ...
    if(condition){
	    if (Time.time >= nextTime) {
            MyMethod();
            nextTime = Time.time + sendRate; 
        }
    }
}

Thanks.

@WiS3 Just set a variable to be equal to Time.deltaTime. Everytime it gets over 0.1 call your function and set the variable to 0.

	float tempTime;
	void Update () {
		tempTime += Time.deltaTime;
		if (tempTime > 0.1) {
                    tempTime = 0;
			function();
		}
	}

function Start ()
{
InvokeRepeating(“FunctionName”, 0, 0.1);
}

function FunctionName ()
{
    if (conditionIsMet)
    {
        //call other function that actually has the code
        //or put the code in this function
    }
}

Like this?

To get more precision and less accumulative errors you should replace:

nextTime = Time.time + sendRate; 

with

nextTime += sendRate;

The reason is you enter the if statement when Time.time is greater than your desired execution time. If you use the current time + your sendRate you add this little “overshooting amount” to your delay. However if you add the sendRate to the old time you always get an evenly spaced call count.

With this approach you have to make sure to initialize sendRate with a good start value. The best thing is to add a security check like this:

     if (Time.time >= nextTime) {
         MyMethod();
         nextTime += sendRate;
         if (Time.time >= nextTime)
             nextTime = Time.time + sendRate;
     }

That way if nextTime “lags way behind” it will be set to a fix point in the future. This is important if you can “disable” this check so Time.time can go way beyond the last nextTime value. Without that check it will call your method each frame until nextTime catches up with the current time when reenabled.