Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

unity3d

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)

like image 342
r0128 Avatar asked Nov 22 '16 21:11

r0128


People also ask

How do I find a specific child of a GameObject in Unity?

The simplest way to get a child object of a game object in Unity is to use the Find method of the Transform class, i.e. transform. Find(“Child Object's Name”). This method will return the target child object which you can then perform various things with.

How do I access my GameObject kids?

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.

How do you reference another GameObject script?

Simply attach your script with the public reference to a Game Object in your scene and you'll see the reference in the Inspector window. Then you can drag any other object that matches the type of your public reference, to this reference in the Inspector and you can use it in your script.


1 Answers

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>();

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
}
like image 82
Programmer Avatar answered Oct 01 '22 01:10

Programmer