how to script a timed GUI Label into my script

Hey guys,

I have this script so far, and what i want to do is make a timed GUI Label, so that when the player comes into range of the “objective” variable (this script is attached to the player) then it displays a GUI Label that only lasts for a couple of seconds…

var objective : Transform;
    
    function OnGUI{
    
    if (Vector3.Distance(transform.position, objective.position) < 3000){
    
    }
    }

I have tried using the yield method (http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html), but that doesn’t seem to work entirely…

any answers would be greatly appreciated!!!

-Grady

var objective : Transform;
var showLabel : boolean = false;
var timeUntilHide : float;

function Update()
{
   if (Vector3.Distance(transform.position, objective.position) < 3000 )
   {
     if ( !showLabel )
         StartCotourine("ShowAndWaitUntilHide");
   }
   else if ( showLabel )
   {
     StopCoroutine("ShowAndWaitUntilHide");
     showLabel = false;
   }
}

function OnGUI
{
   if ( showLabel )
     GUI.Label(Rect(10,10,20,20), "MyText");
}

function ShowAndWaitUntilHide()
{
  timeUntilHide = 5;

  showLabel = true;

  while ( timeUntilHide > 0 )
  {
    // Wait 1 frame before returning to this loop
    yield;

    timeUntilHide  -= Time.deltaTime;
  }

  showLabel = false;
}