I can not explain this:
child.GetComponent<MeshRenderer>().material=MyMaterial; //work
child.GetComponent<MeshRenderer>().materials[0]=MyMaterial; //Not work
WHY??
I want to assign a material to all childrens:
//For all chidrens
if(child.GetType() == typeof(MeshRenderer)){
child.GetComponent<MeshRenderer>().material=MyMaterial;
// >> WORK for first material (ID 0)
// for (int i = 0; i < child.GetComponent<MeshRenderer>().materials.Length; i++)
// {
//
// child.GetComponent<MeshRenderer>().materials*=MyMaterial;*
// }
// >> NOT WORK
// foreach (Material mat in child.GetComponent().materials){
//
// mat=MyMaterial;
//
// }
// >> error CS1656: Cannot assign to mat' because it is a
foreach iteration variable’
-
}*