- Home /
Alpha not displaying when changed in code, unless changed manually in the inspector
Hello there !
I've come across many problems with alpha in unity, and with a lot of reading I've manage to get rid of most of it.. but one is still there :
I've made this script :
public class Alpha : MonoBehaviour {
public Renderer r;
void Start()
{
if(r == null) r = GetComponent<Renderer>();
r.material.SetFloat("_Mode", 2.0f);
}
IEnumerator FadeOut()
{
float a = 1;
while(a > 0)
{
a -= 0.01f;
Color c = r.material.color;
c.a = a;
r.material.color = c;
yield return new WaitForEndOfFrame();
}
StartCoroutine("FadeIn");
}
IEnumerator FadeIn()
{
float a = 0;
while(a < 1)
{
a += 0.01f;
Color c = r.material.color;
c.a = a;
r.material.SetColor("_Color", c);
yield return new WaitForEndOfFrame();
}
StartCoroutine("FadeOut");
}
}
Then, I instantiate an object and put this script on it, and call StartCoroutine :
go = GameObject.Instantiate(go, p.Location, Quaternion.Euler(p.Rotation)) as GameObject;
go.name = (p.isMobile ? "mob_" : "item_") + p.Serial.ToString();
Renderer[] rs = go.GetComponentsInChildren<Renderer>();
for(int i = 0; i < rs.Length; i++)
{
Alpha a = rs[i].gameObject.AddComponent<Alpha>();
a.r = rs[i];
a.StartCoroutine("FadeIn");
}
And this doesn't work ! The value alpha value changes in the inspector, but nothing change in the scene. Then if I manually change the alpha in the inspector, the display works and the object starts fading in and out as it should...
Any idea?
I have the same question - hoping someone knows the answer! I can set the alpha using a script, and the Inspector shows the proper value during Play, but the object in the game is not reflecting the new alpha. If I slightly move the alpha then the object in the game updates and works perfect.