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 ();
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 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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BlinkingTextCorutina : MonoBehaviour
{
Text flashingText;
void Start()
{
flashingText = GetComponent<Text>();
Debug.Log("Start Coroutine");
StartCoroutine(BlinkText());
Debug.Log("End Coroutine");
}
// 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(.5f);
Debug.Log("After YIELD");
//display “I AM FLASHING TEXT” for the next 0.5 seconds
flashingText.text = "I AM FLASHING TEXT!";
yield return new WaitForSeconds(.5f);
}
}
}
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;
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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BlinkingTextCorutina : MonoBehaviour
{
Text flashingText;
string textToBlink;
public float BlinkTime;
void Awake()
{
flashingText = GetComponent<Text>();
textToBlink = flashingText.text;
BlinkTime = 0.5f;
}
void OnEnable()
{
StartCoroutine(BlinkText());
}
IEnumerator BlinkText()
{
while (true)
{
flashingText.text = textToBlink;
yield return new WaitForSeconds(BlinkTime);
flashingText.text = string.Empty;
yield return new WaitForSeconds(BlinkTime);
}
}
}
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.
It works like this too:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BlinkingTextCorutina : MonoBehaviour
{
Text flashingText;
string textToBlink;
public float BlinkTime;
void Start()
{
flashingText = GetComponent<Text>();
textToBlink = flashingText.text;
BlinkTime = 0.5f;
StartCoroutine(BlinkText());
}
IEnumerator BlinkText()
{
while (true)
{
flashingText.text = textToBlink;
yield return new WaitForSeconds(BlinkTime);
flashingText.text = string.Empty;
yield return new WaitForSeconds(BlinkTime);
}
}
}