# Creating an animation for materials in code (Unity)

I have a function where you click on an object and it changes the color and emission of a specific material, every object has multiple materials with different order.

var emissionColor = mat.GetColor("_EmissionColor");

foreach (var m in materials)
{
if (m.name.Equals(matName))
{
//Debug.Log(m.name);
m.EnableKeyword("_EMISSION");
m.color = mat.color;
m.SetColor("_EmissionColor", emissionColor);

}
}


So far I'm changing the color once but I would like to have it transition into a different color every 10 seconds, I tried using the animator but it keeps changing the wrong material. Is there a way to do this animation in code?

I didn't want to delete this question because it might help someone else facing this problem. Basically I used the code above in a function (ChangeColor) that I call twice with a different mat every time. In order to get that delay I used Invoke("ChangeColor", delay); for the second time that I change the material's color.