Newb coroutines question

Hi guys - my first post.

Looking forward to getting into Unity. My biggest stumbling block is coroutines… just don’t seem to be getting it. The best way to describe my question is by asking why the code below doesn’t work. What seems to be happening is flicker() is being called every frame so several flicker () threads are running in parallel…how would this be best amended?

Thanks! :slight_smile:

function Update () {

flicker();

}

function flicker()
{

yield WaitForSeconds(1);

light.enabled = false;

yield WaitForSeconds(1);

light.enabled = true;

}

Don’t call flicker() from Update, which runs once every frame. If you want it to run once, just call it from Start. If you want it to repeat, also use Start (as a coroutine) and an infinite loop:

function Start () {
    while (true) {
        yield WaitForSeconds(1);
        light.enabled = !light.enabled;
    }
}

–Eric

Cool, thanks Eric