0
\$\begingroup\$

Shader

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" ?

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ And if performance is a concern (as changing material.color will clone the material) you can use MaterialPropertyBlocks \$\endgroup\$ – Draco18s Feb 15 '18 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.