- Home /
How to change all material elements of object's children
So my problem is that my game object changes half of its materials and the other half is not affected. I want to change all of it and I need to do it by accessing the object's children. Any help would be appreciated.
Here is what I did so far..
var newMat : Material;
function Start() {
ChangeMaterial();
}
function ChangeMaterial() {
var children : Renderer[];
children = GetComponentsInChildren.<Renderer>();
for (var i : Renderer in children) {
i.material = newMat;
}
}
Is this code on the parent object? Are all the children active?
the only thing I could imagine is that he loop isn't finished the right way, in which case you should do a for loop with increments For example: for(var i = 0;i
This code is on the parent object. I changed the for loop like your example and I get the same results.
Answer by robertbu · Mar 01, 2014 at 08:42 PM
Rereading your question, I'll bet your meshes have more than one material assigned. You will need to cycle through and reset all the materials on the mesh. Try this (untested):
function ChangeMaterial(newMat : Material) {
var children : Renderer[];
children = GetComponentsInChildren.<Renderer>();
for (var rend : Renderer in children) {
var mats = new Material[rend.materials.Length];
for (var j = 0; j < rend.materials.Length; j++) {
mats[j] = newMat;
}
rend.materials = mats;
}
}
Yes my mesh has more than one material assigned. I think I did reset the materials on the mesh and modified the script according to your example but it doesn't seem to change anything.
I edited my answer. Apparently you cannot replace individually replace materials in the 'materials' array. You have to create a new array and replace the whole thing. Try the edited code.
Sorry to bring this old answer up but how would you write this in C#? I'm migrating all my code and having trouble with the syntax. Thank You.
Answer by Fritsl · Nov 01, 2017 at 02:50 PM
c# for the lazy :
void ChangeMaterial(Material newMat)
{
Renderer[] children;
children = GetComponentsInChildren<Renderer>();
foreach (Renderer rend in children)
{
var mats = new Material[rend.materials.Length];
for (var j = 0; j < rend.materials.Length; j++)
{
mats[j] = newMat;
}
rend.materials = mats;
}
}
Answer by metixgosu · Mar 16, 2021 at 07:53 PM
This code works but dont forget to add your parent game object name like.
void ChangeMaterial(Material newMat)
{
Renderer[] children;
GameObject parent;
children = parent.GetComponentsInChildren<Renderer>();
foreach (Renderer rend in children)
{
var mats = new Material[rend.materials.Length];
for (var j = 0; j < rend.materials.Length; j++)
{
mats[j] = newMat;
}
rend.materials = mats;
}
}
hi metixgosu,
could u maybe help me with my thread?
https://forum.unity.com/threads/unity-xr-radial-menu-button-material-manager.1220454/
Your answer
Follow this Question
Related Questions
What's wrong with this array assignment? 1 Answer
Can I check the name of an element? 2 Answers
Array of named booleans 2 Answers
Find Child of GameObject from Array in Different Script 0 Answers
renderer.material doesnt work 3 Answers