- Home /
StartCoroutine troubles, not delaying/
I have a sprite that goes through waypoints by the Patrol function. I have
transform.LookAt(target, Vector3.up);
velocity = moveDirection.normalized * speed;
Which controls the movement of the sprite, if I comment it out, my sprite does not move. But when I have a Co-routine right before it, which delays for 9 seconds, but it is not delaying anything (but it's going into the coroutine as my "YEP" shows in debug)
if (delayWaypoint.Length > 0)
StartCoroutine("checkForDelay");
transform.LookAt(target, Vector3.up);
velocity = moveDirection.normalized * speed;
and the coroutine code:
private IEnumerator checkForDelay()
{
int i = 0;
foreach(int num in delayWaypoint)
{
if(num==currentWaypoint)
{
Debug.Log("YEP:" + delayAmount[i]);
yield return new WaitForSeconds(delayAmount[i]);
}
i++;
}
}
So when I run it, in debug this is shown "YEP:9" but nothing is delayed.
hmm it seems like my logic needs to be in the coroutine for it to delay. that seems a bit silly
Answer by gruhm · Oct 24, 2011 at 11:37 AM
The code in a Coroutine after the yield return is going to run concurrently with the method that called it, So the delay effects code within the coroutine called after the yield.
If you simply want to delay execution for 9 seconds, take a look at Invoke.
Answer by PatHightree · Oct 23, 2011 at 04:43 PM
Make sure you call your coroutine with :
StartCoroutine(checkForDelay());
@PatHightree I have tried both checkForDelay() and "checkForDealy" both work the same :(
$$anonymous$$y bad, I didn't read your calling code... just blurted out my own most common mistake ;)
Your answer
