- Home /
The question is answered, right answer was accepted
Setting emission color programatically
I have tried searching for this, but cannot find any solution. I'm setting the color of each object as such:
Color color = Color.red;
float f = 0.4f;
GetComponent<Renderer>().material.SetColor("_EmissionColor",color*f);
However, in the game the color is not set unless i select the object and expand the material component. [I made a gif illustrating what I mean.][1]
I have tried using the DynamicGI, based on issues that were kind of similar, but nothing works. However, seeing that clicking the material in the unity editor causes it to be updated, the change seems to be registered. The updated materials are just not automatically applied.
EDIT: Finally, after searching for altogether probably 8 hours, I found the solution. If you have the same issue, add
material.EnableKeyword("_EMISSION");
after setting the emissioncolor :) [1]: http://i.imgur.com/dQKeyBN.gif
Answer by grisungen · Nov 12, 2015 at 04:24 PM
I solved the problem. Turns out I had to also set material.color, in addition to the emissioncolor.
i.e:
void Start(){
renderer.material.color = color;
}
void Update(){
f = getIntensity();
Color color = Color.red * f;
renderer.material.SetColor("_EmissionColor", color);
}
Answer by grisungen · Nov 12, 2015 at 04:27 PM
Finally, after searching for altogether probably 8 hours, I found the solution. If you have the same issue, add material.EnableKeyword("_EMISSION"); after setting the emissioncolor :)
Thank you so much for saving me these 8 hours! It turned out to be the problem for me.