Unity – Change Text UI alpha color from script?
Try using Lerp. As long as you set the ‘alpha’ of ‘newColor’ to 0, text will fade out.
public Text example;
public Color newColor;
public float fadeTime = 0.1f; //maybe rename this to fadeSpeed
//this should be called somewhere in Update
void FadeOut()
{
example.color = Color.Lerp(example.color, newColor, fadeTime * Time.deltaTime);
}
EDIT: Using coroutines
void CallingMethod()
{
StartCoroutine(FadeOut());
}
//note the change from 'void' to 'IEnumerator'
IEnumerator FadeOut()
{
//ugly while, Update would be ideal
while (example.color.a > 0)
{
example.color = Color.Lerp(example.color, newColor, fadeTime * Time.deltaTime);
yield return null;
}
//code after fading is finished
}
Read more here: Source link