Changing material at runtime of a meshrenderer won't work

When I change during runtime the material of a mesh renderer with another material using the same shader,
the properties of my material stay at the same value as the former material but the name of the material of the meshrenderer is updated.

I tried

  • obj.GetComponent().mat = newMat;

or

  • obj.GetComponent().mat.color =
    newMat.color;

or

  • obj.GetComponent()…material.SetColor(“_Color”,
    newMat.color);

I don’t understand how it works

You can’t change directly a material in the material array of the object. You need to get the array.
Try below solution.

MeshRenderer myRendMat;
void Start()
{
myRendMat = transform.GetComponent();
ChangeColor();
}

private void ChangeColor()
{
    Material[] material = myRendMat.materials;
    material[0].color = Color.red;
}

Any help ?

Test this:

obj.GetComponent().material = newMaterial;
or
obj.GetComponent().material.color = Color.green; // Or something that you want