How can I get access to the Main Color and change the alpha color ? How do I know what is the variable of the main color in the shader code ?

If it's Main Color in the Inspector then this is the parameter I should get in the script "Main Color" ?


1 Answer 1


If the colour is exposed with the name _Color in the shader code (most defaults are), then you can access it with material.color, using a reference to the material instance you want to change, something like this:

var renderer = GetComponent<Renderer>();
// This getter copies the material to a local instance, 
// so cache it and remember to clean it up when you're done with it.
_cachedMaterial = renderer.material;

// Read, modify the alpha, and write back to the material.
Color color = _cachedMaterial.color;
color.a = 0.5f;
_cachedMaterial.color = color;

If it has a different name in the shader code, you can use material.GetColor() or material.SetColor(), passing either the internal name of the colour variable as a string, or an integer ID retrieved via Shader.PropertyToID().

Please remember to take at least a cursory search through the available API documentation or tutorials for basic questions like this - you'll often find your own answer far faster than it takes someone else to type it up for you.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .