- Home /
Question by
hin899 · Apr 22, 2015 at 01:20 PM ·
shaderproperties
Problem in updating shader properties from script
I have a float property in my shader which I want to change its value from my script.
My shader
Properties {
_Opacity ("Opacity", Range(0.0,1.0)) = 1.0
_MainTex ("Texture to blend", 2D) = "black" {}
}
My script
protected void Update ()
{
float opacity = 0.2f;
Debug.Log(string.Format("BEFORE: {0}", this.renderer.material.GetFloat("_Opacity")));
this.renderer.material.SetFloat("_Opacity", opacity);
Debug.Log(string.Format("AFTER: {0}", this.renderer.material.GetFloat("_Opacity")));
}
The problem is this float variable got reset to value 1.0f every frame entering Update().
Any help or clue would be gratefully received!
Thanks.
Comment
Hi, it might be because the material+shader you are modifying the value is displayed in an inspector. The inspector will reset the value to whatever value is displayed. I got this problem with the standard shader. $$anonymous$$ake sure no inspector is inspecting this material+shader when you run the script.
Hi! I'm having the exact same problem. Did you find, by any chance, a solution, yet?
Answer by SUMFX · Apr 22, 2015 at 02:46 PM
I'm not sure and not tested but you can try this;
void Update ()
{
float opacity = 0.2f;
Debug.Log(string.Format("BEFORE: {0}", this.renderer.material.GetFloat("_Opacity")));
renderer.material.SetFloat("_Opacity", opacity);
Debug.Log(string.Format("AFTER: {0}", this.renderer.material.GetFloat("_Opacity")));
}