A little question about a coroutine

Hey guys…

To be honest, I don´t wuite understand how a coroutine works… so if any of you want to tell me where should I look to learn, it would be great…

Anyway…it seems I am missing something, here, for the GUILabel to blink…

Can you help me out, please?

    void BuyContinues()
    {
        if (gold >= 6)
        {
            continues = continues + 1;
        }
        else
        {
            StartCoroutine (FlashLabel());
        }
    }

    IEnumerator FlashLabel()
    {
        while (true) {
        displayLabel = true;
        yield return new WaitForSeconds(1);
        displayLabel = false;
        yield return new WaitForSeconds(1);
        }
    }

in my OnGUI I have that if displayLabel is true, then the GUILabel is set

if (displayLabel == true)
        {
            GUI.Label(new Rect(Screen.width * 0.01f, Screen.height * 0.7f, Screen.width * .6f, Screen.height * .2f), "BUY GOLD FIRST ", STOst);
        }

It works… But keep flashing forever. If you use a for() instead of a while() you can make it blink only a few time.
And if you call the coroutine another time you have 2 coroutines running and so on…

Edit: I tryed the GUI.Label without the 3rd parameter. Just in case… :slight_smile:

it never flashed on my game… the text appeared but remained as a plain text…

How should I use the for()?

(anyway, I think I know what the problem is… I paused the game (Time.timeScale = 0) while trying and for some reason it remained like that

Yup, I was right, that´s why it never flashed… so, how do I use the for() so I can make it flash only for a few times?

IEnumerator FlashLabel()
{
        for (int i = 0; i < 3; i++) 
        {
            displayLabel = true;
            yield return new WaitForSeconds(1);
            displayLabel = false;
            yield return new WaitForSeconds(1);
        }
}
1 Like

Great!!! … I knew it as a for loop (from AS3) … sorry to bother :slight_smile:

:slight_smile: