Changing Opacity at Runtime

How would I change an object’s material’s main color opacity through a script at runtime?
“renderer.material.color.opacity” doesn’t work because "‘opacity’ is not a member of ‘UnityEngine.Color’.
Any ideas?

renderer.material.color.a

You need to set the object to use a Transparent/whatever shader for it to fade…

color has 4 members, R, G, B, A - in Transparent shaders, A is mapped to opacity

The Color class properties and functions are in the documentation. As Nicholas said, ‘a’ property is the “alpha” (or “opacity”).

1 Like

Thanks guys. I actually looked at that page but was so set on finding “opacity” that I missed the alpha reference.
However, when I tried this script it did not work:
GameObject.Find(“someObject”).renderer.material.color.a = 100;
Unity says “Assigment to temporary”. I don’t get what that means or how to fix it.
Any ideas?

Are you using C#? In C# it is not possible to modify structs inplace when accessed through a property. Thus you would have to set the entire Color at once.

GameObject.Find("someObject").renderer.material.color = Color (1,1,1,0.5);

Or use javascript, where this is of course possible.
Also the alpha value is a float thus goes from 0 to 1 .

Actually, I am using javascript and I tried using
GameObject.Find(“someObject”).renderer.material.color.a = 1;
and got the same message…

You can split it out into two lines like this:

function Start ()
{
   var go = GameObject.Find("someObject");
   go.renderer.material.color.a = .5;
}

That worked. Thank you! I’m very curious as to why, though. I’m trying to learn enough about programming to create stuff with Unity but there’s so much that is just dark water to me.
Anyways, at least I know how to fix it now. Thanks again.