- Home /
Set alpha in Standard Shader
I want to set the alpha of a material with the Standard Shader. I created the following method.
public void SetAlpha(float alpha) {
Material mat = new Material(_meshRenderer.sharedMaterial);
mat.color = new Color(mat.color.r, mat.color.g, mat.color.b, alpha);
_meshRenderer.sharedMaterial = mat;
}
However after using the method the new alpha value is not displayed. When I inspect the Material the new alpha value is shown. Only if I modify the alpha value by hand does the value get updated.
EDIT I already made sure that the render mode is set to transparent in the base material.
Answer by peaveyyyy · Jul 24, 2015 at 02:48 PM
void SetAlpha(float value)
{
Color color = yourgameobject.GetComponent<Renderer>().materials[0].color;
color.a = value;
yourgameobject.GetComponent<Renderer>().materials[0].color = color;
}
Answer by Veldars · Jul 24, 2015 at 01:58 PM
Hi to change the alpha most of time its the alpha of the main texture that you need to change...
Material mat = new Material(_meshRenderer.sharedMaterial);
Color col = mat.mainTexture.color;
col.a = alpha;
mat.mainTexture.color = col;
Hope this help..
arf sorry, I will check in my script how I manage things like that. Here i mix up with sprite...
If you change the color that works ? Or di you try to change only the color directly :
Color color = _meshRenderer.shared$$anonymous$$aterial.color;
color.a = alpha;
_meshRenderer.shared$$anonymous$$aterial.color = color;
Sorry I'm a bit lost without my computer to try by myself...
Yep that works. The strange thing is the inspector shows the correct new alpha value. But only after editing by hand in the inspector it get's updated...
Yes this is really srtrange, what is your Unity version ? Did you try to simply close and reopen Unity ? (Sometime unity do misterious things ^^))
Your answer
Follow this Question
Related Questions
How to blink a color in unity (legacy shaders/difuse) which is the main color., 0 Answers
How to detect if the Device can run the shader ? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to detect if an object is within another object??? (Hide and seek type game). Thanks! 2 Answers