- Home /
How Would I Make A GUI Label Fade After A Certain Amount Of Time?
Hi, I'm trying to make a GUI Label fade out after a certain amount of time and I need some help on how to do it, preferably in JavaScript.
Answer by Statement · Dec 09, 2010 at 12:18 AM
- To make the label transparent you need to change GUI.color.
- To wait a fixed time, use coroutines with WaitForSeconds or alternatively Invoke.
- Use a coroutine to reduce color alpha over time.
Some javascript:
var color : Color;
function Start() { color = Color.white; yield FadeOutAfterTime(3); }
function OnGUI() { GUI.color = color; GUILayout.Label("Hai!"); // You probably want to restore GUI.Color here }
function FadeOutAfterTime(time : float) { yield WaitForSeconds(time); yield Fade(); }
function Fade() { while (color.a > 0) { color.a -= Time.deltaTime; yield; } }
It's better to use "yield" ins$$anonymous$$d of "yield 0", which adds some boxing/unboxing overhead. "yield" in JS is the equivalent of "yield return null" in C#, which is better in the same way than "yield return 0". Also, you can leave out "StartCoroutine" in JS; it's done for you if you're calling a coroutine. "float time" should be "time : float". "new" is optional so you can also just do "yield WaitForSeconds(time)".
Your answer
Follow this Question
Related Questions
Timer Between Labels 2 Answers
gui label on/off with timer 1 Answer
How can I individually fade the background (and not the GUI)? 1 Answer
How to set text(timer) to a fixed place 1 Answer
How to make 3D Text fade in and out? 3 Answers