- Home /
Question by
Imhotebkin · Mar 28, 2016 at 12:19 AM ·
scripting problemmaterialsrenderer
Changing all materials of selection from code
Hello! I have this very strange problem with changing materials. So I have this code here:
[MenuItem("Editor/Material/Change mats to other folder")]
static void Change1()
{
if (Selection.objects.Length > 0)
for (int k = 0; k < Selection.objects.Length;k++ )
{
for (int i = 0; i < ((GameObject)Selection.objects[k]).GetComponent<Transform>().childCount; i++)
{
if ((GameObject)Selection.objects[k]).transform.GetChild(i).GetComponent<MeshRenderer>() != null)
for (int j = 0; j < ((GameObject)Selection.objects[k]).transform.GetChild(i).GetComponent<MeshRenderer>().sharedMaterials.Length; j++)
{
Material m = ((GameObject)Selection.objects[k]).transform.GetChild(i).GetComponent<MeshRenderer>().sharedMaterials[j];
string[] lookFor = new string[] { "Assets/Mats/Material dump" };
string[] s = AssetDatabase.FindAssets(m.name, lookFor);
Material mmm = (Material)Resources.Load(m.name, typeof(Material));
((GameObject)Selection.objects[k]).transform.GetChild(i).GetComponent<MeshRenderer>().share
dMaterials[j] = mmm;
MonoBehaviour.print(AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(s[0]), typeof(Material)).name);
MonoBehaviour.print(j + " " + s[0] + " " + AssetDatabase.GUIDToAssetPath(s[0]));
}
}
}
}
This should work. It finds all the materials, and it finds the same materials in the dump folder. The change of materials gives no errors. But after the script all of the materials remain the same. What am I missing here? Sorry for the lack of variables, I just had to be sure it's not making a copy of the object and working on that copy
Comment