- Home /
change layer of child
Does anyone know how to access the layer of a child and dynamically change it?
The code I have right now only changes the parent, and not the child. It tags the children well enough, so I may single them out for layer change.
gameObject.tag = "myTag";
for (var child : Transform in transform) {
child.tag = "myTag";
}
var moveLayer : GameObject;
moveLayer = gameObject.FindWithTag("myTag");
moveLayer.layer = 11;
I tried the following, but layers are not part of transform.
for (var childl : Transform in transform) {
childl.layer = 11;
}
How would one access them?
Answer by syclamoth · Sep 19, 2011 at 04:16 AM
To change the layer of a child transform's gameObject, use
child.gameObject.layer = LayerMask.NameToLayer("WhateverYouCalledIt");
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.
Thanks for this, I was racking my brains on how to target child layers, this is it. The second bit of code I wasn't able to get running, I'm also not sure what name is doing, does "String" need to be replaced?
Anyhow, I can make move of the grandchild objects into children and use the first bit of code just fine, thanks again.
The working example for C# which also changes the layer of a transform itself:
public static void ChangeLayersRecursively(this Transform trans, string name)
{
trans.gameObject.layer = Layer$$anonymous$$ask.NameToLayer(name);
foreach(Transform child in trans)
{
child.ChangeLayersRecursively(name);
}
}
vitaly. 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 $$anonymous$$ethods. I want to see a working example of your call "child.ChangeLayersRecursively(name)" within the foreach loop of your declared $$anonymous$$ethod "ChangeLayersRecursively(this Transform trans, string name)"
Or you declared another method elsewhere that you can apply to a transform object.
This can explain your convenient answer "it uses extension $$anonymous$$ethods".
However what you give as a "working example" will only work if you give where the method "ChangeLayersRecursively(name)" comes from.
In the actual state , the use of this method return "the method ChangeLayersRecursively is not a member of the Transform object."
So Please provide the full working example with your Extension class that I can see how you achieved this :)
And I say that because I used your code as part of my program and your code only work if I put "ChangeLayersRecursively(child, name)" in the foreach loop ins$$anonymous$$d of an unknow method of a Transform Object.
Answer by YasinJavaid_ · Mar 05, 2019 at 07:37 AM
Hello simple way to change layer in all child
// 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.
}
Answer by XcentY · Oct 07, 2014 at 01:39 PM
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
It is not wrong. It uses Extension $$anonymous$$ethods, the feature of C# http://msdn.microsoft.com/en-us/library/vstudio/bb383977(v=vs.110).aspx
Depends on where you add the code for us we add this to a static class as opposed to extending. We also changed this take a GameObject and gave it an overload to pass layer as string or int.
Find our adjustments below might be useful for someone.
public static void ChangeLayers(GameObject go, string name)
{
ChangeLayers(go, Layer$$anonymous$$ask.NameToLayer(name));
}
public static void ChangeLayers(GameObject go, int layer)
{
go.layer = layer;
foreach (Transform child in go.transform)
{
ChangeLayers(child.gameObject, layer);
}
}
@XcentY: Just to be clear for anyone reading this post; Vitaly's answer is not wrong, and your answer is not "better". His is simply different than yours. Extensions are a great feature of C#. Check it out ;)
Answer by keshav9999 · Nov 24, 2019 at 06:53 PM
This worked for me
foreach (var partChild in part.part.GetComponentsInChildren()) { partChild.gameObject.layer = 13;//layer number here },This worked fo me.
Answer by Eric5114 · Nov 25, 2019 at 07:54 AM
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)"