Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I decrease opacity in unity?

I see this subject in stack over flow but I think it is false 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.

How I can use:

gameObject.GetComponent<Renderer> ().material.color.a = 0;
like image 534
infinity Avatar asked Sep 05 '15 16:09

infinity


1 Answers

For those who might still come across this question, gameObject.GetComponent<Renderer> ().material.color is not a variable. Create a variable as such:

var trans = 0.5f;
var col = gameObject.GetComponent<Renderer> ().material.color;

Then assign your value:

col.a = trans;

Also be aware that not all shaders have a _Color property. In my case, I had to use:

var col = gameObject.GetComponent<Renderer> ().material.GetColor("_TintColor");
like image 155
thefreeline Avatar answered Sep 24 '22 19:09

thefreeline