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:

dQKeyBN

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 :slight_smile:

I solved the problem. Turns out I had to also set material.color, in addition to the emissioncolor.

void Start ()
{
    renderer.material.color = color;
}

void Update ()
{
    f = getIntensity();
    Color color = Color.red * f;
    renderer.material.SetColor("_EmissionColor", color);
}

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 :slight_smile:

@irin

On your script , before make changes in material create an new material property block example

Public class EmissionColorChanger : Monobehaviour

Private MaterialPropertyBlock mt;
Public Renderer r;

Public void Start() 
{ mt = new();}
Public void changeMaterialproperties() 
{
 r.GetPropertieBlock(mt);
mt.//any changes;
r.SetpropertyBlock(mt);
}

//sorry i am busy so i like this typed check some spelling mistakes there’s