- Home /
Material Color only changes when expanding material tab in inspector.
I created a script, changing the color of a specific object. When I run the game, it doesn't change the color. But when I extend the material tab inside the inspector at runtime, the color does change.
Why does this happen? And is there a way to resolve this problem?
Here's my code: function Start () { if(GameObject.FindGameObjectWithTag("Player").GetComponent.().colorSwitch){
if(sideToCollide == 0){
GetComponent.<Renderer>().material.SetColor("_EmissionColor", GameObject.FindGameObjectWithTag("Player").GetComponent.<player>().leftColor);
}
if(sideToCollide == 1){
GetComponent.<Renderer>().material.SetColor("_EmissionColor", GameObject.FindGameObjectWithTag("Player").GetComponent.<player>().rightColor);
}
}
}
Answer by Jordi-Bonastre · Jan 20, 2016 at 03:44 PM
You must enable the correct Keywords for your required Standard Shader variant. In your case, you should use the keyword "_EMISSION"
Check this link to the manual: http://docs.unity3d.com/Manual/MaterialsAccessingViaScript.html
Answer by BastiaanGrisel · Sep 08, 2020 at 08:42 AM
Look at this thread: https://forum.unity.com/threads/i-modify-the-standard-shader-emission-in-code-it-not-work-in-standalone-build.345150/
You need to change the emission color to something non-black in the inspector (before running) because otherwise the Emission code is left out of the shader for optimisation reasons and you won't be able to change it during runtime.
Your answer
![](https://koobas.hobune.stream/wayback/20220612060945im_/https://answers.unity.com/themes/thub/images/avi.jpg)