- Home /
Fading out GUI Label using coroutine problem
Hi, like many people here i'm new to Unity and scripting in general so forgive my naivety :)
What i'm attempting to do is have a GUI label appear and fade out over time once my player enters a trigger box. Following on from this question and answer:
http://answers.unity3d.com/questions/16756/guitexture-fade-in-out-at-time.html
...i can get it to work fine however it only works the once and it looks like i need to somehow reset the co-routine each time(?) This is the script that controls the GUI element:
static var Greeting : boolean;
var color : Color;
function Start(){
color = Color.white;
yield FadeOutAfterTime(3);
}
function OnGUI () {
if (Greeting) {
GUI.color = color;
GUI.Label (Rect (Screen.width / 2 - 50, Screen.height / 2 - 125, 100, 30), "Hello");
}
}
function FadeOutAfterTime(time : float){
yield WaitForSeconds(4);
yield Fade();
}
function Fade(){
while (color.a > 0)
// slowed the fade out
{color.a -= Time.deltaTime/3;
yield;
}
}
And this one I use for the collider trigger:
function OnTriggerEnter (col : Collider) {
FadeUI.Greeting = true;
}
Relatively straight forward i'm sure to many however I'm just wondering what i'm missing since it works fine once but not a second time when i walk back into the trigger.
I've tried various things, indeed i can turn the GUI label on and off effectively with an exit and enter collider quite easily (hence the booleon) but was wanting a nice fade effect.
Answer by Bunny83 · Aug 06, 2011 at 11:25 AM
Ehm, quite obvious :D
You start your coroutine in Start() so it is called only once. You should start a new coroutine and you also have to reset the color when it happens again.
var Greeting : boolean;
var color : Color;
function Start()
{
ShowGreetings();
}
function OnGUI()
{
if (Greeting)
{
GUI.color = color;
GUI.Label (Rect (Screen.width / 2 - 50, Screen.height / 2 - 125, 100, 30), "Hello");
}
}
function ShowGreetings()
{
Greeting = true;
color = Color.white;
yield WaitForSeconds(3);
while (color.a > 0)
{
color.a -= Time.deltaTime/3;
yield;
}
Greeting = false;
}
Just call ShowGreetings if you want to show them again. If you want to call it from another script you either send a message with SendMessage to the object or use
GetComponent.<FadeUI>().ShowGreetings();
The easiest way is to give the script on the trigger a public var in which you can drag&drop your "FadeUI script":
var myFadeScript : FadeUI;
function OnTriggerEnter (col : Collider)
{
myFadeScript.ShowGreetings();
}
Answer by aldonaletto · Aug 06, 2011 at 12:01 PM
It's better to change the whole thing - a coroutine isn't the best choice in this case. You must detect when Greeting changes to true, then start the fading process. When the fading ends, clear Greeting, so it will be ready for the next time:
static var Greeting : boolean = false; private var lastState: boolean = false; private var startFade: float = 0; var color : Color;
function OnGUI () {
if (Greeting != lastState){ // has Greeting changed? lastState = Greeting; // update lastState if (Greeting){ // if Greeting changed to true color = Color.white; // reload color startFade = Time.time + 4; // and define time to start fading } } if (Greeting) { GUI.color = color; GUI.Label (Rect (Screen.width / 2 - 50, Screen.height / 2 - 125, 100, 30), "Hello"); } }
function Update(){
if (Greeting && Time.time > startFade){ // is time to fade? if (color.a > 0){ // fade to zero with time // slowed the fade out color.a -= Time.deltaTime/3; } else { Greeting = false; // when fading ended, reset Greeting } } }
Thanks aldonaletto, i had wondered if there was another way to do what i was trying, the previous answer i reference in my question obviously did what the poster wanted to do and just about did what i wanted.
I'll certainly be trying out both solutions, cheers guys.
Don't $$anonymous$$d, now you have two different solutions to the same problem. Both answers probably will do what you want, with $$anonymous$$or differences: @Bunny83's is more elegant and concise, and kept coroutines like the original idea, while $$anonymous$$e kept the control by the Greetings variable, but became more cumbersome.
Your answer
Follow this Question
Related Questions
FadeIn/FadeOut GUI Menu and Text 1 Answer
Repeat fade in and out 1 Answer
slowly make gui image more transparent 2 Answers
Can I get a prefab to render in front of GUI buttons created in script? 1 Answer
Fade GUITexture 1 Answer