I’m trying (and failing) to write an editor script in c# to apply materials to children of game objects. I usually use javascript so forgive the newbie question.
I have one that applies them to gameobjects but I can’t get it to work with just the children.
Given this example in the help file
HingeJoint[] hingeJoints = GetComponentsInChildren<HingeJoint>();
for (HingeJoint joint in hingeJoints) {
joint.useSpring = false;
}
I thought this would work
Renderer[] children = go.GetComponentsInChildren<Renderer>();
for (Renderer rc in children) {
rc.material = material;
}
But I always get the error
for the second line.
What is wrong here?
I’ve searched the forum and found about 100 ways to do this but all of them give me the same error.
Good to see you have found a solution. As to the compile error in your C# script above the reason is the “for” statement. In C# to iterate through an array without using indexes you need to use “foreach”:
Renderer[] children = go.GetComponentsInChildren<Renderer>();
foreach (Renderer rc in children) {
rc.material = material;
}