- Home /
Material SetColor in a loop not working on Android
Ok, so I'm being blinded by something obvious here, so I'm really hoping a fresh pair of eyes can help me out!
I have a custom shader with a _ColourBoost Color property as follows:
Shader "CameraFeed" {
Properties {
...
_ColourBoost ("Colour Boost", Color) = (0.2,0.4,0.2,1.0)
...
I can set that property in the material from a C# script, and it works as you'd expect when run in both the editor and on the Android device I'm targetting:
CameraMaterial.SetColor("_ColourBoost", new Color(0.39F,0.5F,0.39F));
But, if i try to update that value in a loop (e.g. a fade starting from black and ending at the chosen colour), something like this:
private float progress = 0.0f;
while(progress<1) {
CameraMaterial.SetColor("_ColourBoost", new Color(0.39F*progress,0.5F*progress,0.39F*progress));
progress += Time.deltaTime/6; // Fade in over 6 seconds
}
If I play in the editor, it appears as I expect - fading the colour in from black over 6 seconds. But when deployed to my Android device, it apparently does nothing - the material doesn't change and remains exactly the same over the course of the loop.
If it makes a difference, the loop is contained in an OnGUI function, but what I really don't understand is why the behaviour in the editor doesn't match that on the device. Any ideas what I'm missing here? Thanks!
Answer by tanoshimi · Sep 11, 2013 at 02:53 PM
Solved it (although I don't quite understand how/why). A separate script was responsible for setting the _MainTex property on this same material, as follows:
renderer.material.SetTexture("_MainTex", cameraTexture);
changing this to:
renderer.sharedMaterial.SetTexture("_MainTex", cameraTexture);
fixed the issue. Strange.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113948im_/https://answers.unity.com/themes/thub/images/avi.jpg)