Execute a function for ten seconds and disable it for two seconds

I have a particular function which I am calling in Update function. For the first ten seconds, the function should get called in Update function, then it should get disabled for next two seconds, then again enable it for next ten seconds. This cycle should keep repeating? How can I execute it?

Alternatively, there is one-liner using modulus:

void Update()
{
//if you want it loop from specific start time rather than from start of the game,
//subtract said time value from Time.time argument value
if(Mathf.Repeat(Time.time, execDuration + sleepDuration) < execDuration)
executeFunction();
}

• Is there a difference between Repeat() and % (modulus)? The documentation says "this is similar to the modulo operator but it works with floating point numbers", but modulus works with floats... – BlueRaja - Danny Pflughoeft Jun 7 '17 at 15:19
• @BlueRaja-DannyPflughoeft yes and no - as far as I know in various languages the % operator often act weirdly - either doesn't work with floating point numbers, gives unexpected or outright incorrect results for modulus operation in its math meaning (reflecting hardware nature of the operation on integers). Repeat() was chosen just as a safer option to avoid necessity of looking up exact implementation of % operator in C#/mono. – wondra Jun 7 '17 at 15:30
• I thought every language implemented the IEEE 754 standard, but TIL the standard's "modulo" definition is unintuitive so almost no languages implement it. – BlueRaja - Danny Pflughoeft Jun 7 '17 at 16:27
• It would be better if you implemented the comment over code as another code block and titled them accordingly. – S. Tarık Çetin Jun 11 '17 at 11:11

I haven't tested the following code, but you will get the idea :

public float wakeUpDuration = 10.0f ;
public float sleepDuration = 2.0f;
private bool callFunction = true ;
private float time = 0 ;

void Update()
{
time += Time.deltaTime;
if( callFunction )
{
if( time >= wakeUpDuration )
{
callFunction = false;
time = 0 ;
}
else
{
}
}
if( !callFunction && time >= sleepDuration )
{
callFunction = true;
time = 0 ;
}
}

• That should only work if deltaTime is relatively short. If the delta is longer than sleepDuration then this will fail. – James Curran Jun 7 '17 at 13:40
• if sleepDuration > Time.deltaTime, then, you don't need such system. – Hellium Jun 7 '17 at 13:44
• No. Say deltaTime is 2.75 seconds. You wil have 3 or 4 "active" calls then one "sleep" call, until the 50 second mark, when you should have 8 active calls in a row. Also, in that scenario, the first sleep call comes at 11 seconds -- you reset time to 0 -- it should be set to 1. – James Curran Jun 7 '17 at 14:09
• In Unity, Time.deltaTime represents the elapsed time between the current frame of the game engine and the last frame (thus, I hope for the final user that Time.deltaTime will never be 2.75 seconds, ....). In this context, you may "miss" a call to the function or a "non"-call to the function, which is not an issue here IMHO. – Hellium Jun 7 '17 at 14:20

You can do this with a coroutine also. Something like

public class Comp : MonoBehaviour
{
private bool _shouldCall;

Start()
{
}

Update()
{
if(_shouldCall)
CallTheFunction();
}