Make a custom 3d mesh object opaque and transparent

Hello everyone,

I am very new to Unity3D and C#. I am making a simple application that changes the transparency of the object (as shown in attachment). When I use the Albedo manually it does the great job, but I want to achieve the same thing with the help of a button. That means when the button is clicked, I get the to make the object opaque or transparent through script.

I’ve seen a lot of code such as gameobject.GetComponent<Renderer>().material.color… and so on but nothing seems to work when I click the button. I don’t get any errors either

I’d be grateful if someone can help me achieving the Alpha value from 255 to 0 or vice versa on button click event.

Thank you in advance,
Cheers

Let your button call this method:

Renderer renderer;//drag object here in the inspector

public void SwitchTransparency() {
     material mat = renderer.material;
     float alpha = mat.color.a;
     if(alpha  == 0) {
          alpha = 1;
     }
     else {
          alpha = 0;
     }
     mat.color = new Color(color.r, color.g, color.b, alpha);
     renderer.material = mat;
}