Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting Child of a Child

Discussion in 'Scripting' started by ICS499, Mar 7, 2010.

  1. ICS499

    ICS499

    Joined:
    Feb 20, 2010
    Posts:
    11
    Is this possible?

    I can get the child of a transform and set it active using this GetChild(x).gameObject.SetActiveRecursively(true). But I am trying to first find the right hand of my player, then under the right hand I have weapons which are not active.

    My heirarchy looks like this
    Code (csharp):
    1.  
    2. Player
    3. -Torso/Head (He's a circle)
    4. -L Arm
    5. -R Arm
    6.  -Axe (Inactive)
    7.  -Sword (Inactive)
    8. -L Foot
    9. -R Foot
    10.  
    What I want to achieve is something like this:

    transform.GetChild(3).GetChild(0).SetActiveRecursively(true);


    Calling GetChild on GetChild does not work I've also tried these with no luck:

    transform.GetChild(2).gameObject.GetChild(0).SetActiveRecursively(true);
    transform.GetChild(2).transform.GetChild(0).SetActiveRecursively(true);

    Is there anyway to achieve this?

    Thanks in advance!
    -Anthony
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You probably want to use transform.Find.

    --Eric
     
  3. ICS499

    ICS499

    Joined:
    Feb 20, 2010
    Posts:
    11
    Wow... Thanks. Hahahaha
     
  4. ICS499

    ICS499

    Joined:
    Feb 20, 2010
    Posts:
    11
    Hmm actually I just tried this, and it's giving me the Error:

    'SetActiveRecursively' is not a member of 'UnityEngine.Transform'.

    I tried to do this:
    transform.Find("axe").SetActiveRecursively(true);

    What type of object can SetActiveRecursively be called on? I cant seem to find too much on it in the API.

    **EDIT

    Nevermind I figured it out, thanks for the help

    transform.Find("righthand").transform.Find("axe").gameObject.SetActiveRecursively(true);
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    As described in the docs, you can just use transform.Find("righthand/axe") instead of using multiple Finds.

    --Eric
     
  6. ICS499

    ICS499

    Joined:
    Feb 20, 2010
    Posts:
    11
    Thanks!