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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Blinking text couritine

Discussion in 'Scripting' 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. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    If you want to wait 0.5 seconds, you have to use 0.5f inside your WaitForSecond and not this .00005f
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Code (CSharp):
    1. yield return new WaitForSeconds (.00005f);
    This does not wait for .5 seconds... This waits for 5/100000s of a second... In reality since your game won't run faster than ~60-100 FPS, it will just wait until the next frame.

    Try this:
    Code (CSharp):
    1. yield return new WaitForSeconds (.5f);
     
  4. vtjmbaena

    vtjmbaena

    Joined:
    Apr 10, 2020
    Posts:
    8
    Hi :) !

    Thank for the answers, but problem is not that,
    the problem is that in yield the couritine comes out and the blinking does not repeat. so it only runs the "yield return new WaitForSeconds (.5f)" line once.

    Can you help me know why this comes out?

    Bye
     
  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. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,303
    Are you disabling the Text gameobject, or any of its parents?
    That will shutdown the coroutine. And it won't start again when reenabling, because StartCoroutine is in your Start function, which is only called once.

    Try putting the StartCoroutine in OnEnable
    Code (CSharp):
    1.     private void OnEnable()
    2.     {
    3.         StartCoroutine(BlinkText());
    4.     }
    You will have to move your initializer code from your Start function to an Awake function instead though, to avoid null reference errors, as OnEnable is called before Start but after Awake

    Code (CSharp):
    1.  
    2. [RequireComponent(typeof(Text))]
    3. public class BlinkText : MonoBehaviour
    4. {
    5.     public float BlinkTime;
    6.     private string textToBlink;
    7.     private Text textComponent;
    8.    
    9.     void Awake()
    10.     {
    11.         textComponent = GetComponent<Text>();
    12.         //Set the Text that will be blinked from the inspector of the text component
    13.         textToBlink = textComponent.text;
    14.     }
    15.  
    16.     private void OnEnable()
    17.     {
    18.         StartCoroutine(Blink());
    19.     }
    20.  
    21.     IEnumerator Blink()
    22.     {
    23.         while (true)
    24.         {
    25.             textComponent.text = textToBlink;
    26.             yield return new WaitForSeconds(BlinkTime);
    27.             textComponent.text = string.Empty;
    28.             yield return new WaitForSeconds(BlinkTime);
    29.         }
    30.     }
    31.  
    32. }
    33.  
     
    Last edited: May 29, 2020
    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!!! ;););)