GUI Label - Blinking Text

How do I make blinking text with GUI Label? or is it easier to do it with an image? if I intend to use GUI Label (say, for localization purpose), do I have to script it manually or there is a function which make life easier? thanks.

Use a boolean and a coroutine to flip it on and off.

//Warning: Forum Code!

private var displayLabel = false;

function Start() {
FlashLabel();
}

function FlashLabel() {
	
// Fancy pants flash of label on and off	
while (1) {
 displayLabel = true;
 yield WaitForSeconds(.5);
 displayLabel = false;
 yield WaitForSeconds(.5);	
}

}

function OnGUI() {

 if (displayLabel == true)
 GUILayout.Label("I AM FLASHING");

}

Something like that. Being able to drive your GUI with an in-line state machine is flexible and powerful. More I use the Unity GUI system the more it grows on me.

1 Like

cooooolll, thank you!

Thanks a lot man it works!!!

I normally do this with a timer and comparing the module.

int t=0;
...
       
void OnGUI(){
    t++;
                if (t % 100 < 50) {
                        GUI.Label (new Rect (Screen.width / 2, Screen.height / 1.4f, 100, 100), "PRESS START", startSt);
                }
}

You can use the Coroutines and new Unity 4.6 GUI to achieve this very easily. Check this article here

Blinking Text - TGC

If you just need the code, here you go

  using UnityEngine;
  using System.Collections;
  using UnityEngine.UI;
  using UnityEngine.EventSystems;
 
  public class FlashingTextScript : MonoBehaviour {
 
  Text flashingText;
 
  void Start(){
  //get the Text component
  flashingText = GetComponent<Text>();
  //Call coroutine BlinkText on Start
  StartCoroutine(BlinkText());
  }
 
  //function to blink the text
  public 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);
  //display “I AM FLASHING TEXT” for the next 0.5 seconds
  flashingText.text= "I AM FLASHING TEXT!";
  yield return new WaitForSeconds(.5f);
  }
  }
 
  }
4 Likes

Thanks, GluedBrain :slight_smile:

Thanks @GluedBrain - just used your method you mentioned and it worked like a charm:

For those who want to see a different script - here is mine and how I used it

So when player dies, the text I entered in the Inspector for _gameOverText
[SerializeField]
private Text _gameOverText;

Will appear and start blinking every 0.5 seconds

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

public class UIManager : MonoBehaviour
{
    [SerializeField]
    private Text _scoreText;
    [SerializeField]
    private Text _gameOverText;

    public static int scoreValue = 0;//when you start game, score is 0
    [SerializeField]
    public Image _LivesImg;
    [SerializeField]
    private Sprite[] _liveSprites;

    private void Start()
    {
        _gameOverText.gameObject.SetActive(false);
    }

    void Update()
    {
        _scoreText.text = "Score: " + scoreValue;
    }

    public void UpdateLives(int currentLives)
    {
        _LivesImg.sprite = _liveSprites[currentLives];
        if (currentLives == 0)
        {
            StartCoroutine(displayGameOver());
        }
    }

    IEnumerator displayGameOver()
    {
        while (true)
        {
            _gameOverText.gameObject.SetActive(true);
            yield return new WaitForSeconds(.5f);
            _gameOverText.gameObject.SetActive(false);
            yield return new WaitForSeconds(.5f);
            //break;
        }
    }
}

Thank you GluedBrain