Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help with Layer change in all Children

Discussion in 'Scripting' started by Mortalanimal, Nov 18, 2019.

  1. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    558
    Hi, you know how when you change a layer in the Inspector and it asks you weather you want to make the same change for all of this children Layers also. I would Like to how to implement this when I change the layer in code.

    Thanks in advance.

    Here is my current Code, but atm it does not change the layer of the children.

    Code (CSharp):
    1. HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();
    2.                 tempSave.gameObject.layer = 19;
    3.  
    4.                 Debug.Log(tempSave.gameObject.layer);
     
  2. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    Something like this should work:

    Code (CSharp):
    1.  
    2.          foreach (Transform child in transform)
    3.          {
    4.                  child.gameObject.layer = 19;
    5.          }
     
  3. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    558

    Love you, thanks bro
     
  4. Tymac

    Tymac

    Joined:
    Nov 12, 2015
    Posts:
    10
    This works for 1 level of children but if there are nested children you need to accomplish this via recursion. This example work set all nested children's layer to the correct value:

    Code (CSharp):
    1. private void SetGameLayerRecursive(GameObject _go, int _layer)
    2.         {
    3.             _go.layer = _layer;
    4.             foreach (Transform child in _go.transform)
    5.             {
    6.                 child.gameObject.layer = _layer;
    7.  
    8.                 Transform _HasChildren = child.GetComponentInChildren<Transform>();
    9.                 if (_HasChildren != null)
    10.                     SetGameLayerRecursive(child.gameObject, _layer);
    11.              
    12.             }
    13.         }
     
    Last edited: Apr 28, 2021
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    "To understand recursion, you must first understand recursion."

    I actually favor the "get all Transforms in children" approach.

    Code (CSharp):
    1.     void SetLayerAllChildren(Transform root, int layer)
    2.     {
    3.         var children = root.GetComponentsInChildren<Transform>(includeInactive: true);
    4.         foreach (var child in children)
    5.         {
    6. //            Debug.Log(child.name);
    7.             child.gameObject.layer = layer;
    8.         }
    9.     }
    10.  
    Bonus: works with RectTransforms transparently.
     
  6. Carrot7

    Carrot7

    Joined:
    Aug 23, 2019
    Posts:
    2
    Thank you, this was very useful
     
    Kurt-Dekker likes this.
  7. TheGonzoGamer

    TheGonzoGamer

    Joined:
    Apr 24, 2019
    Posts:
    35
    Found helpful in early 2023. Still wondering why Unity doesn't just add a flag to gameobject.layer to include children.

    gameobject.layerAllChildren perhaps
     
    wirsbo64 likes this.
  8. sami_unity775

    sami_unity775

    Joined:
    Oct 27, 2022
    Posts:
    1
    foreach (GameObject gameObject in GetComponentsInChildren<GameObject>())
    {
    gameObject.layer = 1;
    }
     
  9. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    This doesn't make much sense and would not compile at all since GameObject is not a component and therefore can not be returned by GetComponentsInChildren. Also even when done properly by using a component that each child has (Transform is the best choice here) your solution would be the same what Kurt suggested 2 years ago.
     
  10. ignacio-casal

    ignacio-casal

    Joined:
    Aug 21, 2013
    Posts:
    8
    I did it similarly, just a bit simpler:

    Code (CSharp):
    1.         private void SetGameLayerRecursive(GameObject gameObject, int layer)
    2.         {
    3.             gameObject.layer = layer;
    4.             foreach (Transform child in gameObject.transform)
    5.             {
    6.                 SetGameLayerRecursive(child.gameObject, layer);
    7.             }
    8.         }
     
    ali12000 likes this.