Search Unity

Changing layer of the children

Discussion in 'Scripting' started by hellcaller, May 9, 2012.

  1. hellcaller

    hellcaller

    Joined:
    May 19, 2010
    Posts:
    381
    Hi, i've bumped into a problem, i have an enemy (default layer) character which after death is switched to ragdoll which should have another layer, so i'm switching enemy's layer via script:
    Code (csharp):
    1.  
    2. ...
    3. currentRagdoll.layer = 12;
    4. Physics.IgnoreLayerCollision(8, 12, true);
    5. ...
    6.  
    but the problem is that the children of the "enemy" do not change their layer... When i'm changing layer in the inspector it asks me if the changes should be done to the children of the object. How to do it via script.
     
  2. hellcaller

    hellcaller

    Joined:
    May 19, 2010
    Posts:
    381
    solution was simple...
    Code (csharp):
    1.  
    2. ...
    3.     children = currentRagdoll.GetComponentsInChildren<Transform>();
    4.         foreach (Transform go in children) {
    5.         go.gameObject.layer = 12;
    6.     }
    7.  
     
  3. rxmarccall

    rxmarccall

    Joined:
    Oct 13, 2011
    Posts:
    353
    I know this is way old, but wanted to say thanks!
     
  4. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    238
    I know this is way way way old, but wanted to say thanks too!
     
  5. HaromFarkas

    HaromFarkas

    Joined:
    Mar 7, 2020
    Posts:
    13
    I know this is way way way way old, but wanted to say thank you kindly!
     
    HeyBishop likes this.