Looping through materials on a mesh with foreach() in Start() is causing lasting FPS drop?

Hi,

I’ve got a simple script which on Start() creates 1000 cubes with 6 materials each (one for each face) from a prefab to just test out some performance stuff. I was trying to loop through the materials and turn the shader off by script basically but I was actually not finding an increase in FPS in my test scenes.

I found that just having this line on my cube’s Start() class is having a huge hit on the performance even after all the cubes have finished their Start methods:

private void Start()
{
    foreach (Material m in GetComponent<Renderer>().materials)
    {

    }
}

Just having this empty loop in the Start method means even after the start menu on my test scene I go from about 80ms to 200ms, could anyone explain what’s going on and maybe a more efficient way I can handle this?

Accessing the materials property will basically unhook this renderer from using the shared materials and create cloned materials so you can access the material, change values without affecting any other. See this Remember that unity can batch draw calls when they use the same material, by accessing materials you clone every material and therefore every object has a different material.

Now you could access sharedMaterials but obviously changing anything inside it will change it for every other object aswell.