Fade out Unity UI text

Isaac Adni picture Isaac Adni · Jan 11, 2015 · Viewed 22.9k times · Source

When I run the following code on my UI text

Color color = text.color;
color.a -= 1.0f;
text.color = color;

The alpha value of the text is immediately set to 0. How can I simply fade out the text.

Answer

Jonathan picture Jonathan · Dec 13, 2015

If you are using Unity 4.6 and newer you can take advantage of CrossFadeAlpha and CrossFadeColor.

Example:

// fade to transparent over 500ms.
text.CrossFadeAlpha(0.0f, 0.05f, false);

// and back over 500ms.
text.CrossFadeAlpha(1.0f, 0.05f, false);

These two functions are a bit nicer to use since you don't have to worry about keeping track of anything. Just call it and go about your day.