x


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.

more ▼

asked Oct 23 '11 at 04:40 PM

cookies gravatar image

cookies
173 11 13 16

hmm it seems like my logic needs to be in the coroutine for it to delay. that seems a bit silly

Oct 23 '11 at 08:40 PM cookies
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Oct 24 '11 at 11:37 AM

gruhm gravatar image

gruhm
121 1 2

(comments are locked)
10|3000 characters needed characters left

Make sure you call your coroutine with :

StartCoroutine(checkForDelay());
more ▼

answered Oct 23 '11 at 04:43 PM

PatHightree gravatar image

PatHightree
379 7 10 20

@PatHightree I have tried both checkForDelay() and "checkForDealy" both work the same :(

Oct 23 '11 at 04:57 PM cookies

My bad, I didn't read your calling code... just blurted out my own most common mistake ;)

Oct 24 '11 at 07:30 AM PatHightree
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x323
x179

asked: Oct 23 '11 at 04:40 PM

Seen: 705 times

Last Updated: Oct 24 '11 at 11:38 AM