0
\$\begingroup\$

I have already tried really much with all "GetChildren" and similar functions, but I haven't found out how to do it yet.

I have a GameObject named "koffer". I have added several other GameObjects as children to it.

I have added a script to "koffer" in which I try to retrieve the children. In this script I have stated this line:

    string sThis = this.name; //just for testing. "sThis" becomes "koffer", so it looks perfectly fine.
    foreach (GameObject g in this.GetComponentsInChildren<GameObject>()) // doesn't retrieve anything. So obviously I'm not doing it correctly.

However this doesn't retrieve anything. Is this not the correct function for such a case?

Is there any built-in function to retrieve theses game obects?

I have found several custom made functions from some years ago, and I don't know if they are still needed or if Unity has integrated some functions for that.

What really bothers me is that there are so many custom-made functions that deal with "Transform" instead of "GameObject". Why would somebody need Transform instead of GameObject?

Thank you.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ GameObject is not a Component, so it cannot be returned by GetComponent or its ilk. The reason the scripts you've found for the purpose of navigating children use Transform is that the Transform component is the one that manages the parent-child relationships you're trying to navigate here. GameObjects do not have parents or children directly, only their Transform component does. That's why the tree of nested objects is called a "Transformation Hierarchy" \$\endgroup\$
    – DMGregory
    Commented May 3, 2019 at 2:35
  • \$\begingroup\$ @DMGregory Best explanation ever! Perfectly understandable. Thank you! \$\endgroup\$
    – tmighty
    Commented May 3, 2019 at 18:30

2 Answers 2

5
\$\begingroup\$

GameObjects do not have children per-se. When we talk about children to a GameObject we are actually referring to the children of the Transform component (that all GameObjects have). This means that you have to use the Transform component to access the children:

void Start() {
    // All GameObjects have a transform component built-in
    foreach(Transform child in this.transform) {
        GameObject obj = child.gameObject;

        // Do things with obj
    }
}

The reason you can't use GetComponentsInChildren<T>() to get the children is simply because that is not the functionality of that function. That function (like the name says) is used to get components from children (not the children themselves). For example (taken from Unity documentation):

void Start()
{
    var hingeJoints = GetComponentsInChildren<HingeJoint>();
    foreach (HingeJoint joint in hingeJoints)
        joint.useSpring = false;
}

However I guess you can technically use this function to get all children:

void Start()
{
    // This only works because GameObjects always have the Transform component,
    // and there can only be 1 transform component per GameObject
    var transforms = GetComponentsInChildren<Transform>();
    foreach (Transform transform in transforms) {
        GameObject obj = transform.gameObject;

        // Do something with obj
    }
}
\$\endgroup\$
1
\$\begingroup\$

you can use transform.GetChild(0) you can also get children of children transform.GetChild(0).GetChild(0)

or you can pull a array of each child and work with that also

    for (int i = 0; i < transform.childCount; i++)
    {
        //do stuff

    } 

or

    foreach (Transform child in transform)
    {
        // do stuff
    }
\$\endgroup\$
5
  • \$\begingroup\$ Seconds before I saw your posting I went crazy about the fact that people post functions dealing with Transform. Now you do the same. Can you tell me why? I talk about GameObject, and you go for Transform. Why? \$\endgroup\$
    – tmighty
    Commented May 3, 2019 at 2:26
  • \$\begingroup\$ I was hoping I would find out how to go through the children using something like this: foreach (GameObject g in this.GetComponentsInChildren < GameObject >()). I have tried so much, but I haven't found a working solution yet. \$\endgroup\$
    – tmighty
    Commented May 3, 2019 at 2:27
  • \$\begingroup\$ because the transform has has those methods like transform.GetChild(0) and stuff. you can say Gameobject go = child.gameObject or transform.gameObject \$\endgroup\$ Commented May 3, 2019 at 2:30
  • \$\begingroup\$ Why should I use "GetChild(0)"? Can I not use GetComponentsInChildren<GameObject>()? \$\endgroup\$
    – tmighty
    Commented May 3, 2019 at 2:33
  • \$\begingroup\$ the Transform class is typically the preferred way. transform.gameObject would be pretty much the same as GetComponent<GameObject>(). but also grabbing a child using transform.GetChild(0).gameobject.GetComponent<Animator>(); will work but if you try that the other way you could most likely have a null reference if you are pulling that info from something from another script without a reference to the go. \$\endgroup\$ Commented May 3, 2019 at 3:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .