How to change the Alpha channel of a GUI button?

Hello everyone,

I’m trying to change the Alpha channel of one of my GUI buttons, so when I hit the button the alpha changes the amount to “0” and disappears. Also I want to change the speed of it.

Please take a look to my script, any advice is welcome.

var Skin_buttonOne : GUISkin;
var Skin_buttonTwo : GUISkin;
var beep : AudioClip;
    
function OnGUI(){
 
	GUI.color.a = 1;
	
  	GUI.skin = Skin_buttonOne;
	if (GUI.Button(Rect(870,470,80,39),"")){
    audio.PlayOneShot(beep);     
    GUI.color.a = 0;
  	
	}

	GUI.skin = Skin_buttonTwo;
	if (GUI.Button(Rect(870,512,80,80),"")){
    audio.PlayOneShot(beep);
 
	}
}

@script RequireComponent(AudioSource)

You should have a boolean flag to enable alpha decrement: set it to true when the button is pressed, so that the button starts vanishing after being pressed. Alpha must be decremented in Update, since Time.deltaTime must not be used inside OnGUI (it shows the time between frames, not between OnGUI calls). Additionally, you should stop drawing the button when it’s invisible to avoid embarrasing clicks!

var Skin_buttonOne : GUISkin;
var Skin_buttonTwo : GUISkin;
var beep : AudioClip;
var duration : float = 1.5; // time taken to disappear
private var alpha: float = 1; // alpha starts at 1
private var vanish = false;

function Update(){
  if (vanish){ // only decrement alpha after button pressed
    alpha -= Time.deltaTime/duration;
  }
}

function OnGUI(){
  GUI.color.a = alpha; // set alpha for this button
  if (alpha > 0){ // only draw the button while it's visible
    GUI.skin = Skin_buttonOne;
    if (GUI.Button(Rect(870,470,80,39),"")){
      audio.PlayOneShot(beep);
      vanish = true; // enable alpha decrement
    }
  }
  GUI.color.a = 1; // restore alpha for other GUI items
  GUI.skin = Skin_buttonTwo;
  if (GUI.Button(Rect(870,512,80,80),"")){
    audio.PlayOneShot(beep);
  }
}

@script RequireComponent(AudioSource)