如何通过脚本查找游戏对象的子对象或附加到子游戏对象的脚本 [英] How to find child of a GameObject or the script attached to child GameObject via script
问题描述
我知道这是一个有点愚蠢的问题,但是我将如何通过脚本(脚本附加到游戏对象)引用游戏对象的子项(立方体).(相当于 GetComponent 之类的东西)
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)
推荐答案
通过索引查找子游戏对象:
您可以使用 GetChild
函数.
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;
您可以通过向 GetChild
函数.
You can get other children by providing index number of the child GameObject such as 1,2,3, to the GetChild
function.
如果它是原始游戏对象中孩子的孩子?我会不会必须重复它或者它只是第n个下降
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.
假设这是 OriginalGameObject/Prefab 层次结构:
Let's say this is OriginalGameObject/Prefab hierarchy:
- 原始游戏对象
- child1
- child2
- child3
- childOfChild3
如您所见,
OriginalGameObject
是child1
、child2
和child3
的父级.childOfChild3 是child3
的孩子.As you can see the
OriginalGameObject
is the parent ofchild1
,child2
andchild3
. childOfChild3 is the child ofchild3
.假设您想访问
childs
并且您只有对OriginalGameObject
的引用,它是父游戏对象:Let's say you want to access the
childs
and you only have reference toOriginalGameObject
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;
索引从
0
开始,所以真正的索引号是index-1
,就像数组一样.The index starts from
0
so the real index number isindex-1
just like arrays.现在,要获取
childOfChild3
的引用,它是child3
的子代,但您只能引用OriginalGameObject
,它是父 GameObject:Now, to get the reference of
childOfChild3
which is the child ofchild3
but you only have reference toOriginalGameObject
which is the parent GameObject:首先获取
child3
的引用,然后从中获取childOfChild3
.First of all, get reference of
child3
then getchildOfChild3
from it.GameObject mychild = originalGameObject.transform.GetChild(2).gameObject; GameObject childOfChild3 = mychild.transform.GetChild(0).gameObject;
通过循环索引查找[所有]子游戏对象:
遍历 originalGameObject 的所有子对象:
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 }
您还可以使用
transform.FindChild
函数按名称查找孩子.我不建议你这样做.当多个孩子共享相同的名字时,它看起来很慢并且会发生冲突.这就是您使用GetChild
的原因.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 useGetChild
.按名称查找子游戏对象:
GameObject child1 = originalGameObject.transform.FindChild("child1").gameObject; GameObject child2 = originalGameObject.transform.FindChild("child2").gameObject; GameObject child3 = originalGameObject.transform.FindChild("child3").gameObject;
要查找
childOfChild3
,您可以使用'/
' 轻松找到,就像使用文件目录一样.您提供parent/child
然后名称.childOfChild3
的父对象是child3
.所以,我们在 FindChild 函数中使用childOfChild3/child3
.To find
childOfChild3
, you can easily do that with the '/
' just like you do with a file directory. You provide theparent/child
then name. The parent ofchildOfChild3
ischild3
. So, we use,childOfChild3/child3
in FindChild function.GameObject childOfChild3 = originalGameObject.transform.FindChild("child3/childOfChild3").gameObject;
查找附加到子游戏对象的脚本/组件:
如果您只想要附加到子游戏对象的脚本,请使用 <代码>GetComponentInChildren:
If all you want is the script that is attached to the child GameObject, then use
GetComponentInChildren
:MyScript childScript = originalGameObject.GetComponentInChildren<MyScript>();
如果有多个孩子使用相同的脚本,而您只想获得所有附加到他们的脚本,则使用
GetComponentsInChildren
: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 }
这篇关于如何通过脚本查找游戏对象的子对象或附加到子游戏对象的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!