- Home /
Question by
schnarch33 · Oct 18, 2017 at 02:15 PM ·
shaderrenderertransparencyalphaalpha-channel
Cutout Shader getting Opaque when turning alpha off and on again
Hi there,
I'm currently having problems with Unity's builtin default cutout shader. Every time I make an object (that has this shader on it) invisible and then visible again by changing the alpha channel of the material's color, the transparent parts of the object become opaque.
Does anybody know why this happens and how I can avoid it?
My Code (Ignore the thin with the irgnorationVector):
private Renderer rend;
private void Start()
{
rend = this.GetComponent<Renderer>();
ownCollider = this.GetComponent<Collider>();
}
//gets called by another script
public void handleTransparency(Transform mainCamera)
{
if (ignorationVector == Vector3.zero || !Vector3Helper.checkIfCollinear(mainCamera.forward, ignorationVector))
{
setTransparency(0f);
}
else
{
setTransparency(255f);
}
}
public void setTransparency(float alpha)
{
if(rend == null)
{
return;
}
if(alpha > 255)
{
alpha = 255;
}
if(alpha < 0)
{
alpha = 0;
}
Color color = rend.material.color;
color.a = alpha;
rend.material.color = color;
}
Before:
After:
before.png
(16.5 kB)
after.png
(1.9 kB)
Comment
Answer by schnarch33 · Dec 02, 2017 at 09:51 PM
Got it!
For all who are still having problems: the alpha channel has to be set to 1 and not to 255.