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 :slight_smile:

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.

Ehm, quite obvious :smiley:
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();
}

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
    }
  }
}