Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

GetComponentsInChilderen and GetComponentsInParent only recursive in depth

Discussion in 'Scripting' started by onurmuhurdar, Jun 3, 2019.

  1. onurmuhurdar

    onurmuhurdar

    Joined:
    Jun 3, 2019
    Posts:
    3
    Hi, maybe I am not correct on the programming lingo here but what I was trying to say is this:


    I have a humanoid game object and the hips are the parent of everything. In some cases, for example the legs and the arms, they are stacked on the same row so if the code accesses one it won't access the other. Is there a way to make this work easily in Unity without a workaround, if not how can I make this work other than making everything stack under each other?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,794
    Could you show a screenshot of the Hierarchy window to demonstrate what kind of setup this is, and clarify what exactly you are trying to do with this setup?
     
  3. onurmuhurdar

    onurmuhurdar

    Joined:
    Jun 3, 2019
    Posts:
    3

    This is my normal hierarchy for the humanoid enemies:
    not working.jpg
    And this is what I came up with that works, but for the sake of keeping this neat and clean I just want to be able to handle things in code:
    works.jpg
    What I am doing is making the kinematic body pieces go non-kinematic for a fancy ragdoll effect. Also the game does different calculations according to what limb is shot for a semi-realistic response. (eg. legs or arms disable the enemy while chest shots depending on caliber puts them on "about to die from bleeding" state) I'm doing that in code with different tags on collision btw. I've thought of manually finding and disabling kinematics but thats just too bulky.
     
  4. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    837
    You want to get all the components in the entire character?
    You can use Transform.root to get at the top most node in your hierarchy. Then you can use GetComponentsInChildren to get at everything from that root.
     
    onurmuhurdar likes this.
  5. onurmuhurdar

    onurmuhurdar

    Joined:
    Jun 3, 2019
    Posts:
    3
    This seems to have works, in which case the problem was me using the "Get Components in Parents" rather than then childeren. Thanks!