Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Coroutine only runs once from button

Discussion in 'Scripting' started by bullettea, Aug 26, 2023.

  1. bullettea

    bullettea

    Joined:
    Oct 24, 2019
    Posts:
    29
    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:
    Code (CSharp):
    1.  clickableTimerText.onClick.AddListener(() =>
    2.         {
    3.             Debug.Log("clicked");
    4.             StartCoroutine(flashingWords());
    5.         });
    6.  
    7.     private IEnumerator flashingWords()
    8.     {
    9.         messageText.text = "PUSHED!";
    10.         yield return new WaitForSeconds(3f);
    11.         messageText.text = "";
    12.     }
    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!
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Ryiah likes this.
  3. bullettea

    bullettea

    Joined:
    Oct 24, 2019
    Posts:
    29
    Thank you for your response. Incidentally, how would you code this without coroutines? Using Invoke?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    I would just have a timer:

    Code (csharp):
    1. private float showWord;
    When the button is clicked:

    Code (csharp):
    1. showWord = 3.0f;
    2. // set message on here...
    In Update():

    Code (csharp):
    1. if (showWord > 0)
    2. {
    3.   showWord -= Time.deltaTime;
    4.   if (showWord <= 0)
    5.   {
    6.     // clear message
    7.   }
    8. }
     
    Ryiah likes this.
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    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. :)
    Code (CSharp):
    1. bool flashing;
    2.     int timer;
    3.  
    4.     clickableTimerText.onClick.AddListener(() =>
    5.     {
    6.         Debug.Log("clicked");
    7.         flashing = true;
    8.     });
    9.    
    10.     void Update()
    11.     {
    12.         if (flashing) { HandleFlashing(); }
    13.     }
    14.    
    15.     void HandleFlashing()
    16.     {
    17.         timer++;
    18.         if (timer <= fps * 3f) // if you have own fps counter
    19.         {
    20.             if (timer == 1) { messageText.text = "PUSHED!"; }
    21.             // other logic...
    22.         }
    23.         else
    24.         {
    25.             messageText.text = string.Empty;
    26.             flashing = false;
    27.             timer = 0;
    28.         }
    29.     }