UI Button Transparency

I've been trying to find a way to make a UI button transparent and all I can ever find is how to make it transparent and clickable, etc, etc, not a script that can make it transparent or non transparent. Can someone show me how I would make this work?

void Update(){

public Button leaveButton;
public Button backButton;

if (Input.GetKeyDown (KeyCode.Escape)) {
            //Make buttons non transparent
        }
}
public void backToGame(){ //This would be attached to the backButton
            //Make buttons transparent
}

Thanks for anyone willing to help, there are so many things I need to fix that this script will help.

That script isn't written correctly.
I'm pretty sure what you're looking for is :

Color col = GetComponent<Image>().color;
col.a = 0; 
GetComponent<Image>().color = col;

This still leaves the text visible, so .. I don't know if you want it a percentage of transparency, but not completely? Just modify the 'col.a =' to something between 0 and 1. :)

1 Like

or add a CanvasGroup Component and modify the alpha value for it to affect the button and its children (including the text).

3 Likes

Another good option :)

If you want the button to disappear. Try
Button.gameObject.setActive (false)

Not sure if anyone is still looking for a solution but I accomplished this by just setting the capacity low on my .png file I was using to display the button.

Bit of a necropost, no idea how you found this. But, assuming you are using like 5.0+ UI, just use a CanvasGroup and CanvasGroup.alpha value to change transparency.

In the inspector, change Color alpha to a value you want, normal button color will be transparent, in script you use this simple code.

 itemImage.GetComponent<Image>().color = Color.white;

The white color will override the alpha or any color you used in the button before. This is because the white color is a default color of the image in Unity standards. White color is RGB(1,1,1,1), if you use RGB(1,1,1,0.4) it always override the value in the inspector to a default state.

I'm not sure if anyone else still has trouble with this.

In Unity 2019.4, select the button > Image tab > color and change the alpha (capital A) value to 0.

The button will be transparent. I'm not sure if it works for other versions earlier than this.

Sorry for the necropost

1 Like


Brilliant and simple, this helped me so much. Thanks.