Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Blinking text coroutine

Discussion in 'Editor & General Support' started by vtjmbaena, May 28, 2020.

  1. vtjmbaena

    vtjmbaena

    Joined:
    Apr 10, 2020
    Posts:
    8
    Hello

    I am trying to do a blinking text with corutine:

    In the text 2d text I have the following example code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class BlinkingTextCorutina: MonoBehaviour
    {
    Text flashingText;
    void Start ()
    {
    flashingText = GetComponent <Text> ();
    StartCoroutine (BlinkText ());

    }

    // Update is called once per frame
    IEnumerator BlinkText ()
    {
    // blink it forever. You can set a terminating condition depending upon your requirement
    while (true)
    {
    // set the Text's text to blank
    flashingText.text = "";
    // display blank text for 0.5 seconds
    yield return new WaitForSeconds (.00005f);
    // display “I AM FLASHING TEXT” for the next 0.5 seconds
    flashingText.text = "I AM FLASHING TEXT!";
    yield return new WaitForSeconds (.00005f);
    }
    }
    }

    But the problem I have is that in the first yield it is exited and it no longer passes to the next one, is that normal?

    I can't get the flicker to be constant.

    bye
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I don't understand what you're saying is happening here. If it is exiting on the first yield and never coming back then your problem wouldn't be that the flicker isn't constant but you're not seeing any flicker at all. Add Debug.Log statements to figure out what is going on with the code. Verify you don't have any runtime errors in the console, and verify that this script and its GameObject indeed are in the scene and active when you notice the flicker isn't working.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
  4. vtjmbaena

    vtjmbaena

    Joined:
    Apr 10, 2020
    Posts:
    8
    If i use debug.log


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class BlinkingTextCorutina : MonoBehaviour
    7. {
    8.     Text flashingText;
    9.     void Start()
    10.     {
    11.         flashingText = GetComponent<Text>();
    12.         Debug.Log("Start Coroutine");
    13.         StartCoroutine(BlinkText());
    14.         Debug.Log("End Coroutine");
    15.      
    16.     }
    17.  
    18.     // Update is called once per frame
    19.      IEnumerator BlinkText()
    20.     {
    21.         //blink it forever. You can set a terminating condition depending upon your requirement
    22.         while (true)
    23.         {
    24.             //set the Text's text to blank
    25.             flashingText.text = "";
    26.             //display blank text for 0.5 seconds
    27.             yield return new WaitForSeconds(.5f);
    28.             Debug.Log("After YIELD");
    29.             //display “I AM FLASHING TEXT” for the next 0.5 seconds
    30.             flashingText.text = "I AM FLASHING TEXT!";
    31.             yield return new WaitForSeconds(.5f);
    32.         }
    33.     }
    34. }

    The log is:
     

    Attached Files:

  5. vtjmbaena

    vtjmbaena

    Joined:
    Apr 10, 2020
    Posts:
    8
    Hi

    Investigating I have seen that the yield retrun null statement works correctly, the problem is when I use new WaitForSeconds (0.5f); it gets out of the coroutine and it no longer works, that is, it doesn't wait half a second.

    I hope you can help me know why new WaitForSeconds (0.5f) doesn't work;

    regards
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    So you've confirmed the object remains in the hierarchy and the object and script both remain active when you expect the coroutine would resume? From your console output you have no evidence the object still exists after Start has executed.
     
    vtjmbaena likes this.
  7. vtjmbaena

    vtjmbaena

    Joined:
    Apr 10, 2020
    Posts:
    8
    hi

    thanks it's already solved

    With the code:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class BlinkingTextCorutina : MonoBehaviour
    7. {
    8.     Text flashingText;
    9.     string textToBlink;
    10.     public float BlinkTime;
    11.  
    12.      void Awake()
    13.     {
    14.         flashingText = GetComponent<Text>();
    15.         textToBlink = flashingText.text;
    16.         BlinkTime = 0.5f;
    17.     }
    18.     void OnEnable()
    19.     {
    20.  
    21.         StartCoroutine(BlinkText());
    22.    
    23.    
    24.     }
    25.  
    26.  
    27.  
    28.     IEnumerator BlinkText()
    29.     {
    30.         while (true)
    31.         {
    32.             flashingText.text = textToBlink;
    33.             yield return new WaitForSeconds(BlinkTime);
    34.             flashingText.text = string.Empty;
    35.             yield return new WaitForSeconds(BlinkTime);
    36.         }
    37.     }
    38. }
    39.  

    And modifying one thing in the parent object that was also wrong I have made it work.
    The problem is that you were enabling the parent object when calling it in another script.:oops:

    It works like this too:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class BlinkingTextCorutina : MonoBehaviour
    7. {
    8.     Text flashingText;
    9.     string textToBlink;
    10.     public float BlinkTime;
    11.  
    12.      void Start()
    13.     {
    14.         flashingText = GetComponent<Text>();
    15.         textToBlink = flashingText.text;
    16.         BlinkTime = 0.5f;
    17.         StartCoroutine(BlinkText());
    18.     }
    19.  
    20.  
    21.  
    22.  
    23.     IEnumerator BlinkText()
    24.     {
    25.         while (true)
    26.         {
    27.             flashingText.text = textToBlink;
    28.             yield return new WaitForSeconds(BlinkTime);
    29.             flashingText.text = string.Empty;
    30.             yield return new WaitForSeconds(BlinkTime);
    31.         }
    32.     }
    33. }
    34.  
    Thank you very much!!! ;););)
     
    Filip8429 likes this.