Text fade Coroutine help

Hello, I was hoping for some advice. I am attempting to use a CoRoutine to fade some text by lowering the Alpha on a CanvasGroup that theText is on. Once the weapon is select the Text should appear and then fade out after 1.5 seconds. I think it is partially working as the Text does disappear, but the fading effect is not noticable. Here are the relevant parts of my code, from Update -

public float gunNameFade = 1.5f;
public CanvasGroup canvasGroup;

public void NameGun()
    {
        UIController.instance.weaponText.text = activeGun.gunName + " eqipped";
        UIController.instance.weaponText.gameObject.SetActive(true);

        gunNameFade -= Time.deltaTime;
        if (gunNameFade <= 0f)
        {
            StartCoroutine(TextFade(3f));
        }

    }

public IEnumerator TextFade(float time)
    {
        while (canvasGroup.alpha > 0)
        {
            canvasGroup.alpha -= Time.deltaTime / time;
            yield return null;
        }
    }

Thank you in advance for any and all advice!

Don’t use a coroutine for this.

Fading, simple and easy:

1 Like

If you’re looking for an easy non-code way to do stuff like this, there’s a free Asset in the Asset Store called LeanTween, which does this and a ton of other animation shenanigans.

1 Like

Thanks for the advice Kurt! I have tried this -

public float gunNameFade = 1.5f;
public CanvasGroup canvasGroup;
float currentAlpha = 1f; 
float desiredAlpha = 0f;
 

public void NameGun()
    {
        UIController.instance.weaponText.text = activeGun.gunName + " eqipped";
        UIController.instance.weaponText.gameObject.SetActive(true);

        gunNameFade -= Time.deltaTime;
        if (gunNameFade <= 0f)
        {
            canvasGroup.alpha = Mathf.MoveTowards( currentAlpha, desiredAlpha, 3f * Time.deltaTime);
        }

    }

But still no joy! It just disappears suddenly! I must be missing something!