10
\$\begingroup\$

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?

\$\endgroup\$
15
\$\begingroup\$

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();
}
\$\endgroup\$
  • 1
    \$\begingroup\$ 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... \$\endgroup\$ – BlueRaja - Danny Pflughoeft Jun 7 '17 at 15:19
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – wondra Jun 7 '17 at 15:30
  • 3
    \$\begingroup\$ I thought every language implemented the IEEE 754 standard, but TIL the standard's "modulo" definition is unintuitive so almost no languages implement it. \$\endgroup\$ – BlueRaja - Danny Pflughoeft Jun 7 '17 at 16:27
  • \$\begingroup\$ It would be better if you implemented the comment over code as another code block and titled them accordingly. \$\endgroup\$ – S. Tarık Çetin Jun 11 '17 at 11:11
6
\$\begingroup\$

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
         {
             foo(); // Your function
         }
    }
    if( !callFunction && time >= sleepDuration )
    {
        callFunction = true;
        time = 0 ;
    }
}
\$\endgroup\$
  • \$\begingroup\$ That should only work if deltaTime is relatively short. If the delta is longer than sleepDuration then this will fail. \$\endgroup\$ – James Curran Jun 7 '17 at 13:40
  • \$\begingroup\$ if sleepDuration > Time.deltaTime, then, you don't need such system. \$\endgroup\$ – Hellium Jun 7 '17 at 13:44
  • \$\begingroup\$ 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. \$\endgroup\$ – James Curran Jun 7 '17 at 14:09
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Hellium Jun 7 '17 at 14:20
4
\$\begingroup\$

You can do this with a coroutine also. Something like

public class Comp : MonoBehaviour
{
  private bool _shouldCall;

  Start() 
  {
    StartCoroutine(UpdateShouldCall)
  }

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

  IEnumerator UpdateShouldCall()
  {
    while(true) 
    {
        _shouldCall = true;
        yield return new WaitForSeconds(10);
        _shouldCall = false;
        yield return new WaitForSeconds(2);
    }
  }

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.