Adding Flashing Text

I’m trying to add some flashing text. Specifically I want my Gold text to flash like in the old Sonic games. I want it to flash when the counter is zero and then stops when a gold bar is picked up. So far, I have this code/coroutine set up in my script, which makes the gold text flash, but I can’t seem to get it to stop once a gold bar is picked up. What can I do? Thanks.

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

public class GameManager : MonoBehaviour
{
    public Text goldText;
    public static int currentGold;
    public bool textBlink = true;

    void Start()
    {
        StartCoroutine("FlashText");
    }

    public void AddGold(int goldtoAdd)
    {
        currentGold = currentGold + 1;
        //Make sure to add the below method in the right place to call it. If it's only done in the Start function, this will only happen at the start and never again. If trying to access a method from another script, use the 'FindObjectOfType'.
        SetCountText();
    }

    public void SetCountText()
    {
        goldText.text = "Gold: " + currentGold.ToString();
        FindObjectOfType<Objectives>().Objective1();
    }

    public IEnumerator FlashText()
    {
        while (textBlink)
        {
            goldText.enabled = false;
            yield return new WaitForSeconds(.5f);
            goldText.enabled = true;
            yield return new WaitForSeconds(.5f);
        }
    }
}

See how you have “while(textBlink)” on line 32? That’s what keeps it going indefinitely.

Therefore you need something else to turn off textBlink (i.e., set it to false).

Another way is to replace your while(textBlink) statement with a for loop that iterates perhaps three or five times.

If you replace exactly line 32 with five-blink for loop, it would look like this:

for( int i = 0; i < 5; i++)

That might be all you need to get you through. You might have weird uneven blink behavior if two instances of that coroutine get stated before the 5 blinks are done.

I assume it should stop blinking when you get a gold bar, making your “currentGold” variable move from 0 to 1.

You can do it like so:

public IEnumerator FlashText() {
        while (currectGold == 0) { //keep looping while no gold

            goldText.enabled = !goldText.enabled; //flip the active state of goldText
            yield return new WaitForSeconds(.5f);// wait .5 seconds
        }
     goldText.enabled = true; // Don't forget to flip it back on incase it was off when exiting the loop!
    }
1 Like

Thank you so much for this. :smile: Typical that it was so simple… :stuck_out_tongue: The only thing I need to work out now is to keep it working if the player dies. The coroutine doesn’t seem to work after dying for some reason.

Coroutines stop working when an object is destroyed. Perhaps you want to make a separate gold-showing object that doesn’t get destroyed?

But… I haven’t set anything to be destroyed within the coroutine.

If you’re using Sparrows post, line 2 will only continue to work when there is zero gold.

If you use my post it shouldn’t care how much gold you have.

I’ll give yours a go and see how it goes. Though I did do a little something that works, though not sure if it’s considered ‘good practise’. Originally, I have the StartCoroutine initiate in the Start function, but this only caused it to trigger on Play and was why after death it doesn’t run it again.
Now, I have the StartCoroutine in the Start function for on Play as well as in the SetCountText function. That appears to help it run the Coroutine again after death.