-1
\$\begingroup\$

I am trying to call another function continuously from my Update() function. So, I have used yield WaitForSeconds(5.0); but, the it stops for the first 5 seconds and it does not stop the next time it goes inside the movement function. Basically, I want to stop for 5 seconds before every movement.

function Update () 
{
    movement();
}

function movement()
{   

    yield WaitForSeconds(5.0);
    //some blocks of code

}

Thank you.

\$\endgroup\$
1
\$\begingroup\$

In your code, movement() is being called every single update resulting in repeatedly calls completing after the first five seconds - this is not what you want. You want to call movement once and have movement repeat itself. There might be a better pattern which if someone else can show would be great, but something like the following should work and get you pointed in the right direction -

JavaScript:

function Start () {
    StartCoroutine("movement");
}

function movement() {
    while (true) {
        yield WaitForSeconds(5.0);
        // some blocks of code
    }
}

C#:

void Start()
{
    StartCoroutine("movement");
}

IEnumerator movement()
{
    while (true) {        
        yield return new WaitForSeconds(5.0f);        
        //some blocks of code
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Great. I am trying on this :) \$\endgroup\$ – O_o Dec 12 '14 at 18:34

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.