I should warn you, however, that iterating through a Transform like this only returns the first layer of children- it doesn’t give you any children that those children might have. If you want to change an entire hierarchy, you should write some kind of recursive function which can be called on any given Transform to change the layers all the way down, like this (apologies in advance for dodgy JS syntax- I usually use C#):
function ChangeLayersRecursively(var trans : Transform, var name : String)
{
for (var child : Transform in trans)
{
child.gameObject.layer = LayerMask.NameToLayer(name);
ChangeLayersRecursively(child, name);
}
}
This will make a deep search of a Transform hierarchy and change every child in it instead of just the first layer.
// obj is your GameObject change it with your
foreach (Transform child in obj.GetComponentsInChildren<Transform>(true))
{
child.gameObject.layer = LayerMask.NameToLayer ("Default"); // add any layer you want.
}
the comment of vitaly is wrong.
child.ChangeLayersRecursively(name);
the method ChangeLayersRecursively is not a member of the Transform object.
ChangeLayersRecursively(child, name); is better
Do you have a working example of what you said ? When I said it was wrong, you replied it is not wrong, it uses extension Methods. I want to see a working example of your call “child.ChangeLayersRecursively(name)” within the foreach loop of your declared Method “ChangeLayersRecursively(this Transform trans, string name)”