- Home /
Changing texture of childrens
Hey,
I have the following setup: I have an empty game object, which has many childs. I would like to create a script, which I can attach to the empty game object, and do the following:
It loops trought the childs, and it changes it's main textures. If the object name is "roadEdge", than it skip the object, otherwise, it changes the main texture.
I have tried to do it, but I cant get it working.
Can anyone help me?
Answer by hololabs · Mar 09, 2013 at 06:44 AM
This might be a more concise way (in C#):
Renderer[] renderers = transform.GetComponentsInChildren<Renderer>();
foreach (Renderer r in renderers)
{
r.material.mainTexture = myTexture;
}
Answer by Ludiares.du · Oct 14, 2011 at 01:20 PM
foreach(Transform[] childs in transform)
{
for(int cnt = 0; cnt < childs.Lenght; childs++)
{
if(childs[cnt].name != roadEdge)
{
childs[cnt].renderer.material.mainTexture = myTexture;
}
}
}
I have copied it to the script, but I have received 10 errors, starting in this line: foreach(Transform[] childs in transform)
I am using javascript.
Your answer
![](https://koobas.hobune.stream/wayback/20220613010613im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Change material of all the children 2 Answers
Loop through textures every half second 2 Answers
change child texture problem 2 Answers
Assigning UV Map to model at runtime 0 Answers
Scroll Texture with Wrap 1 Answer