How to change the transparency of an object in using code?

I have laser-object in my scene, which I want to be seen only when I press the 'E' button on the key board(just an example of a key). I have seen many ways to change the transparency , however, none of them worked. one of them was -

gameObject.renderer.material.color.a = 0.0f ;


I get an Error - UnityEngine.renderer does not contain color and material sub-functions.

I don't know how to do it, and I tried many ways. Does someone have an easy way to make an object transparent and visible in C# script ? Thank you

• In recent unity version you cannot directly assign alpha value, instead you have to make a color object with your desired alpha value. Here is the details youtube.com/watch?v=ALE9Z_wxavg – Muhammad Faizan Khan May 30 '20 at 5:56

Making an object 'transparent' so it cannot be seen is not the most efficient way to do things. What you rather want to do is make the renderer inactive when you don't want to see it, and active when you do.

If you click on your gameObject in the editor, there should be a Mesh Renderer as one of the components.

To set it to inactive from a script attached to this same gameObject, you can do this...

gameObject.GetComponent<Renderer> ().enabled = false;


If you really want to use transparency, you can do this...

gameObject.GetComponent<Renderer> ().material.color.a = 0;


Although if you are setting transparency, you need to make sure the shader the material is using supports transparency. I would suggest using the Legacy Shaders/Transparent Diffuse shader.

• So all I need to do is use this line code(the renderer disabled) - 1 time when I want to see, and 1 time when I dont want? Is that all? – ErezProductions Aug 9 '15 at 23:17
• I did what you said, how ever I still see the objects. Does it work with rigidbody component? – ErezProductions Aug 10 '15 at 0:34
• A rigidbody component and a renderer component are two different things. You need to run the script on a gameobject that has a renderer component such as Mesh Renderer etc. It is this component that lets you see the object. – Greg Quinn Aug 10 '15 at 4:26
• I have done that and it works, however, when I tried to do this again but with the Box Collider, the box collider component isn't turning off even though it is written in the script. – ErezProductions Aug 10 '15 at 12:33

In recent Unity version,

this code is invalid

gameObject.renderer.material.color.a = 0.0f ; instead first you have to get materail from rendere like this

 private Material currentMat;

// Start is called before the first frame update
void Start()
{
currentMat = gameObject.GetComponent<Renderer>().material;

}


Then, use this method to change alpha value

void ChangeAlpha(float alphaVal)
{
Color oldColor = currentMat .color;
Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, alphaVal);
currentMat .SetColor("_Color", newColor);

}


For more explanation, you see this video link.

Disable Objects || Set Objects Transparent

Ok, so I'm by no means an expert but let me take a crack at this.

If you want an object disabled entirely, you would say:

public bool isInvisible;
private void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
if (!isInvisible)
gameObject.SetActive(false);
isInvisible = !isInvisible;
else
gameObject.SetActive(true);
isInvisible = !isInvisible;
}
}


In the Unity editor, within the Inspector window, next to the name of each GameObject there's a little boolean check box. This sets the GameObject to either active or not active, the corresponding line of code in C# being the one listed above within the if statement. This will keep the object instantiated, but render it disabled and all of its components will be disabled as well.

Now if you wanted to stick with changing the alpha, and simply making it transparent, you could try something like this:

public bool isInvisible;
private void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
Color curColor = gameObject.GetComponent<Renderer>().material.color;
if (!isInvisible)
gameObject.GetComponent<Renderer>().material.color = new Color(curColor.r, curColor.g, curColor.b, 0);
isInvisible = !isInvisible;
else
gameObject.GetComponent<Renderer>().material.color = new Color(curColor.r, curColor.g, curColor.b, 1);
isInvisible = !isInvisible;
}
}


This will set the Renderer's current color's alpha to 0, then to 1 respectively as you press "e".

• gameObject.GetComponent<Renderer>().material.color = new Color(curColor.r, curColor.g, curColor.b, (int)isInvisible); might also work for the last bit of code to avoid the nasty if statement :D I'm not expert either :D – I_Keep_Trying Jun 8 at 15:49