Adapting GUI Fade to Objects

I’ve been trying to adapt an existing script that allowed GUI Objects to fade out (and then toggle on later). I’m trying to make it work with an GameObject. Not having the best of luck…

//FadeIn Script for Unity - Zerofractal 2006 
var buttonName = "Toggle Material";
var fadeDuration:float=0.5;
var initialDelay:float=5;
private var timeLeft:float=0.5;
 
function Awake () {
   timeLeft = fadeDuration;
}
 
function Update () {
   if (initialDelay > 0){
      initialDelay = initialDelay-Time.deltaTime;
   } else {
      if (Input.GetButton (buttonName))
         fade(true);
      else
         fade(false);   
   }
}
 
function fade(direction:boolean){
   var alpha;

   
   
   if (direction){
      if (renderer.material.color.a < 0.5){
         timeLeft = timeLeft - Time.deltaTime;
         alpha = (timeLeft/fadeDuration);
         renderer.material.color.a=0.5-(alpha/2);
      } else {
         timeLeft = fadeDuration;
      }
   } else {
      if (renderer.material.color.a > 0){
         timeLeft = timeLeft - Time.deltaTime;
         alpha = (timeLeft/fadeDuration);
         renderer.material.color.a=alpha/2;
      } else {
         timeLeft = fadeDuration;
      }
   }
}

Where have I got that wrong?

Help?

Is the shader you are using on the material in the Alpha group?

It is.
Not sure if I’ve called up that part right with the whole:

renderer.material.color.a

thing. Is that part right?

Try this:

var buttonName = "Toggle Material";
var fadeOutSpeed = 0.4;
var fadeInSpeed = 0.4;

function Update ()
{
	if (Input.GetButton (buttonName))
		Fade (true);
	else
		Fade (false);
}

function Fade (direction : boolean)
{
	if (direction)
	{
		if (renderer.material.color.a > 0.0)
			renderer.material.color.a -= Time.deltaTime * fadeOutSpeed;
	}
	else if (direction == false)
	{
		if (renderer.material.color.a < 1.0)
			renderer.material.color.a += Time.deltaTime * fadeInSpeed;
	}
}