# Yeild waitForSeconds(5.0) is not working

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.

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
}
}

• Great. I am trying on this :) – O_o Dec 12 '14 at 18:34