Coroutine only runs once from button

Ok, wondering if someone could help as I’m tearing my hair out over this. I have button, when pushed, should print out a message for three seconds.

This is the script:

 clickableTimerText.onClick.AddListener(() =>
        {
            Debug.Log("clicked");
            StartCoroutine(flashingWords());
        });

    private IEnumerator flashingWords()
    {
        messageText.text = "PUSHED!";
        yield return new WaitForSeconds(3f);
        messageText.text = "";
    }

The issue here is that it runs fine the first time, and then the text never shows up again on subsequent clicks. Even when Debug.Log shows that the button is indeed being clicked. How do I fix this so the text “PUSHED” shows up EVERYTIME the button is clicked and not just the first time?

Thank you for any help!

I personally don’t use coroutines, so don’t have the slightest idea. Although in this post I found, seems like this guy had the same problem :slight_smile:
https://www.reddit.com/r/Unity3D/comments/1ux009/resetting_an_ienumerator_timer_coroutine/

Thank you for your response. Incidentally, how would you code this without coroutines? Using Invoke?

I would just have a timer:

private float showWord;

When the button is clicked:

showWord = 3.0f;
// set message on here...

In Update():

if (showWord > 0)
{
  showWord -= Time.deltaTime;
  if (showWord <= 0)
  {
    // clear message
  }
}

I personally like to have control over everything, as Coroutines and Invokes run separately from the class, and technically do the same thing as I code anyway. But I can add way more control if needed. :slight_smile:

bool flashing;
    int timer;

    clickableTimerText.onClick.AddListener(() =>
    {
        Debug.Log("clicked");
        flashing = true;
    });
   
    void Update()
    {
        if (flashing) { HandleFlashing(); }
    }
   
    void HandleFlashing()
    {
        timer++;
        if (timer <= fps * 3f) // if you have own fps counter
        {
            if (timer == 1) { messageText.text = "PUSHED!"; }
            // other logic...
        }
        else
        {
            messageText.text = string.Empty;
            flashing = false;
            timer = 0;
        }
    }