Search Unity

Getting children objects?

Discussion in 'Scripting' started by CodingBruh, May 2, 2016.

  1. CodingBruh

    CodingBruh

    Joined:
    May 11, 2015
    Posts:
    114
    Say we have a situation like this on the hierarchy.

    Cube
    -----BabyCube



    How can I access BabyCube (member of the Cube) and access it's properties or components. Thanks :)
     
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    The same way you would anything else. Find, direct reference or if instantiating, caching the reference.
     
  3. CodingBruh

    CodingBruh

    Joined:
    May 11, 2015
    Posts:
    114
    Find? Really? For a child block? Can't I do something like gameObject.BabyCube;


    Example please?
     
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
  5. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    There are several ways, depending on your needs.
    • Transform.Find is great to find a child with a specific name.
    • GetComponentInChildren is great to find a specific component. There is also GetComponentsInChildren
    • Transform.GetChild is great if you know the index
    • And if you just want all the children then Transform itself is iterable, you can simply use foreach (Transform child in transform)
     
    KelsoMRK and zombiegorilla like this.
  7. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Code (csharp):
    1. transform.GetChild(index).GetComponent<>();
     
  8. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    Transform.Find is probably the cleanest option. I typically use gameobject.find(name+"/childname")... But mostly out of habit. Also that method assumes your gameobject has a unique name.
     
    Kiwasi likes this.
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Most of the time I iterate through transform. I find relying on names tends to be fragile. But the standards in my studio are pretty lax.
     
    zombiegorilla likes this.
  10. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    Honestly, I mostly use inspector references, but most of my development is prototyping, so it lets me rewire quickly. It's gets ugly over time though. ;)

    For release code, we primarily use names, but have a very strict naming convention. It is fragile, but our pipeline tools in maya and the asset pipeline enforce and either throw a warning or corrects in some cases. Or as we call it "artist proofing ". ;)

    For our new game the pipeline has been rewritten, and sort of does it similar to unity where everything has an auto generated key that is stored in unity meta, and maya meta, both aren't visible to the user. A data base ties the keys to cms and the names. The goal being that designers will actually browse by assets on web page, and name can change on either end and all the relationships remain. It's early days but so far working smoothly.
     
    Kiwasi likes this.
  11. CodingBruh

    CodingBruh

    Joined:
    May 11, 2015
    Posts:
    114
    Thanks guys!
     
    Rutenis likes this.