Hi, all. I’m trying to fade a 3-D model that has several materials applied to it, all of them using the “Standard” shader. I’m doing this from a script. If the desired alpha level is 1.0 then I simply set the Rendering Mode to 0 (“Opaque”). If the alpha is less than zero, I set Rendering Mode to 2 (“Fade”) and then modify the “_Color” property of the material. This works perfectly… for some of the materials but not others.
What’s really odd is when I run my code, the alpha on some of the materials is changing correctly but over half of them are not changing at all. Now, if I open one of those non-working materials in the Inspector, click on “Albedo”'s eyedropper and then drag its alpha to some value and let go, suddenly the alpha of that material starts magically working from that point.
It should be noted that the alpha on the Albedo of all the materials is changing dynamically each frame but for the non-working materials, it just doesn’t seem to ever get applied until I manually change it.
Can anyone shed light on why some materials would work fine and others need a kick in the butt before they start working? Am I missing an important step? Is this a Unity bug?
Here’s what I’m doing each frame (“myAlpha” is changed elsewhere every frame):
MeshRenderer mr = obj.GetComponent<MeshRenderer>();
for (int i = 0; i < mr.materials.Length; ++i)
{
if (alpha == 1)
{
mr.materials[i].SetFloat("_Mode", 0); // Opaque.
}
else
{
mr.materials[i].SetFloat("_Mode", 2); // Fade.
Color c = mr.materials[i].GetColor("_Color");
c.a = alpha;
mr.materials[i].SetColor("_Color", c);
}
}
Update: Even calling Material.EnableKeyword() when changing rendering modes doesn’t fix the problem. However, if I manually (via the Inspector) set the rendering mode for all materials in the object to “Fade”, the issue goes away when I run the program and all the materials change alpha correctly. I’m still baffled as to what I’m doing wrong in my script.