r0128
r0128

Reputation: 551

How to find child of a GameObject or the script attached to child GameObject via script

I know this is a bit of a stupid question, but how would I reference the child (a cube) of a game object via script(the script is attached to the gameObject). (the equivalent to something like GetComponent)

Upvotes: 42

Views: 208250

Answers (4)

Programmer
Programmer

Reputation: 125455

Finding child GameObject by index:

You can get the first child of a GameObject with the GetChild function.

GameObject originalGameObject = GameObject.Find("MainObj");
GameObject child = originalGameObject.transform.GetChild(0).gameObject;

You can get other children by providing index number of the child GameObject such as 1,2,3, to the GetChild function.

and if its a child of a child in the original gameobject? would I just have to repeat it or would it just be the nth one down

You find the child first, then find the child of that child from the reference.

Let's say this is OriginalGameObject/Prefab hierarchy:

  • OriginalGameObject
    • child1
    • child2
    • child3
      • childOfChild3

As you can see the OriginalGameObject is the parent of child1, child2 and child3. childOfChild3 is the child of child3.

Let's say you want to access the childs and you only have reference to OriginalGameObject which is the parent GameObject:

//Instantiate Prefab
GameObject originalGameObject  = Instantiate(prefab);

//To find `child1` which is the first index(0)
GameObject child1 = originalGameObject.transform.GetChild(0).gameObject;

//To find `child2` which is the second index(1)
GameObject child2 = originalGameObject.transform.GetChild(1).gameObject;

//To find `child3` which is the third index(2)
GameObject child3 = originalGameObject.transform.GetChild(2).gameObject;

The index starts from 0 so the real index number is index-1 just like arrays.

Now, to get the reference of childOfChild3 which is the child of child3 but you only have reference to OriginalGameObject which is the parent GameObject:

First of all, get reference of child3 then get childOfChild3 from it.

GameObject mychild = originalGameObject.transform.GetChild(2).gameObject;
GameObject childOfChild3 = mychild.transform.GetChild(0).gameObject;

Finding [all] child GameObject by index with loop:

To loop through all the child of originalGameObject:

GameObject originalGameObject = Instantiate(prefab);
for (int i = 0; i < originalGameObject.transform.childCount; i++)
{
    GameObject child = originalGameObject.transform.GetChild(i).gameObject;
    //Do something with child
}

You can also find child by name with the transform.FindChild function. I wouldn't recommend that you can do that. It seems slow and will conflict when multiple child share the-same name. That's why you use GetChild.

Finding child GameObject by name:

GameObject child1 = originalGameObject.transform.FindChild("child1").gameObject;
GameObject child2 = originalGameObject.transform.FindChild("child2").gameObject;
GameObject child3 = originalGameObject.transform.FindChild("child3").gameObject;

To find childOfChild3, you can easily do that with the '/' just like you do with a file directory. You provide the parent/child then name. The parent of childOfChild3 is child3. So, we use, childOfChild3/child3 in FindChild function.

GameObject childOfChild3 = originalGameObject.transform.FindChild("child3/childOfChild3").gameObject;

Finding scripts/components attached to child GameObject:

If all you want is the script that is attached to the child GameObject, then use GetComponentInChildren:

MyScript childScript = originalGameObject.GetComponentInChildren<MyScript>();

The above method only fetches one script. If there are more than one child with the-same script, and you just want to get all the scripts attached to them, then use GetComponentsInChildren:

MyScript[] childScripts = originalGameObject.GetComponentsInChildren<MyScript>();
for (int i = 0; i < childScripts.Length; i++)
{
    MyScript myChildScript = childScripts[i];
    //Do something with myChildScript
}

Upvotes: 81

David Sof
David Sof

Reputation: 21

I find this very short method just 2 line, my player hierarchy is like this:

-Player

  1. Head
  2. BulletPos

Code:

Transform[] ObjectChildrens;
ObjectChildrens = gameObject.GetComponentsInChildren<Transform>();
GameObject Head = System.Array.Find(ObjectChildrens, p => p.gameObject.name == "Head").gameObject;


GameObject BulletPos = System.Array.Find(ObjectChildrens, p => p.gameObject.name == "BulletPos").gameObject;
GameObject LifeProgresBar = System.Array.Find(ObjectChildrens, p => p.gameObject.name == "LifeProgresBar").gameObject.GetComponent<Text>().text = "100";
CapsuleCollider2D cc = System.Array.Find(ObjectChildrens, p => p.gameObject.name == "HeadPlace").gameObject.GetComponent<CapsuleCollider2D>();

Upvotes: 1

gabus
gabus

Reputation: 25

You could have public variable in parent gameObject.

public Tranform childGameObject;

and then assign your child gameObject to it.

This way you avoid hidden dependencies. As well as your child cube can be inside of another parent and another parent.

Upvotes: -3

Knetic
Knetic

Reputation: 2127

gameObject.transform.children contains all immediate children of a given GameObject. You can iterate over it and find the desired child. For instance, by name;

for(int i = 0; i < gameObject.transform.childCount;)
{
    if(gameObject.transform.children[i].name == "someName")
    {
        // do what you need with it
    }

}

There are a few ways to iterate. You can use an enumerable, as indicated in these docs, or use FindChild.

Usually you'll want to iterate over children if you have some other criteria, such as wanting to find a child which has a specific type of Component, rather than just by name.

Upvotes: 0

Related Questions