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

Get Child (C#)

Discussion in 'Scripting' started by DethRaid, Mar 28, 2011.

  1. DethRaid

    DethRaid

    Joined:
    Aug 29, 2010
    Posts:
    210
    Is there any way to access the children of an object directly?
     
  2. kamicazer

    kamicazer

    Joined:
    Oct 29, 2010
    Posts:
    32
  3. MegadethRocks

    MegadethRocks

    Joined:
    Dec 12, 2009
    Posts:
    162
    If you know the name of the child object, you can get its transform by doing a transform.FindChild("Child Name"). You can then get the child gameObject by calling .gameObject on the child's transform.

    Code (csharp):
    1. GameObject childGO = transform.FindChild("Child Object").gameObject;
    This code will throw a null reference exception if "Child Object" does not exist, so it's probably better practice to store the child's transform in a temporary variable and make sure it isn't null before trying to get its gameObject.
     
  4. teatime

    teatime

    Joined:
    Jun 16, 2008
    Posts:
    129
    finding and manipulating GameObjects based on their names or position in the transform hierarchy is generally discouraged, as explained here (a little more than 1/4 of the way through, but watch the whole thing if you haven't, it's great):
    http://unity3d.com/support/resources/unite-presentations/techniques-for-making-reusable-code.html

    most of the time doing it will lead to brittle code that not only can't be reused in future projects but has to be changed constantly during the project and often doesn't work because of a mistake made while using the inspector. it's more excusable if the names or hierarchies are created in code, but in that case you could store a reference to the children on creating or modifying them and access it later which would be a lot more efficient than FindChild or GetComponentInChildren. another exception i thought of is for scripts that manipulate character bones, like Rune's Head Look Controller and Locomotion System, but in that case public GameObject/Transform references are much easier and more flexible than getting the child objects in code.

    in short, there might be a more efficient and flexible way to accomplish whatever it is you want to, and we'd be able to help you figure it out with a little more information.
     
    Last edited: Mar 28, 2011
  5. DethRaid

    DethRaid

    Joined:
    Aug 29, 2010
    Posts:
    210
    Well, what I'm trying to do is have a loading bar above a vehicle hanger to represent the progress of training a unit, like in pretty much any RTS game. I'd rather not assign the GUITexture i have for the loading bar to each hanger in the inspector as the player will be able to create more hangers throughout the game. I have the loading bar parented to my hanger prefab. Thus, I thought I could put a bit of code in my Start() function to assign the loading texture to a variable. The loading bar is actually two GUITextures, so using the type won't work.