Destroying leaking materials?

Hi guys!

I am having huge troubles with materials leaking into the scene. I am changing the properties of a lot of materials like changing the textures and changing the colors.

I have found this post http://forum.unity3d.com/viewtopic.php?t=26293 that suggest manually creating and destroying materials when changing properties, but I can’t seem to get it to work.

This is what I am doing:

Material mat = new Material(renderer.material);
renderer.material = mat;

// Function called on Destroy
void Destroy(){
  Destroy(mat);
}

However this doesn’t stop the accumulation of material instances. What am I doing wrong?

Do you call your function from your own code before the object is destroyed? A Destroy function on your object won’t get called automatically. OnDisable will be called just before an object is destroyed, however.