replacing texture before render

I am using this code to change the main texture of the same material in many 3d buttons but am getting this error, what is the proper solution ? (sharedMaterial doesn’t work for my purpose ie: it doesn’t change the texture for each button but for all of them)

void OnWillRenderObject()
{
     renderer.material.mainTexture = texture;
}

ERROR:

Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene. Please use renderer.sharedMaterial instead.[/code]

I’m getting the same error but in different situation. I’m switching materials on objects during runtime… This causes materials names getting weird names composed of multiple “instance”… :frowning:

Well, that’s what the error says: you’re not in Play mode, and want to create an instance of the material (that’s what renderer.material does). Since you’re not in the game, this will leak the material.

Why do you want to be changing colors of some objects when not in Play mode?