- Home /
Question by
importguru88 · Aug 16, 2016 at 12:35 AM ·
texthidetimed
How do I make text stay for a couple of seconds and disappear in the scene after a couple seconds of scene
I am gonna have text in my scene say this :
The score to reach for this level is : 2054
Text stay in the scene for a couple of seconds than disappear in a couple of seconds
Comment
Best Answer
Answer by michael_house · Aug 16, 2016 at 02:03 AM
Use a coroutine and a CanvasGroup to set the opacity.
Something like:
IEnumerator ShowMessageCoroutine(string message, float timeToShow = 10)
{
canvasGroup.alpha = 1;
textElement.text = message;
while (timeShown < timeToShow)
{
timeShown += Time.deltaTime;
yield return null;
}
canvasGroup.alpha = 0;
textElement.text = "";
}
You start it with:
void ShowMessage(string message, float timeToShow = 10)
{
StartCoroutine(ShowMessageCoroutine(message, timeToShow));
}
So I add my text in the code :
textElement.text = "";