Unity寻找子物体的几种方法
1: transform.Find()寻找子物体
transform.Find(“子物体名字”)
2: GameObject.FindGameObjectWithTag() 通过标签获取物体 针对全局
3:transform.GetChild() 通过序号查找子物体
Debug.Log(transform.GetChild(0));
Debug.Log(transform.GetChild(1));
Debug.Log(transform.GetChild(0).GetChild(0));
4:查找子物体的组件
//查找全部
GetComponentsInChildren
Rigidbody rigidbody = GetComponentInChildren<Rigidbody>();
//查找一个
GetComponentInChildren
Rigidbody[] rigidbodys = GetComponentsInChildren<Rigidbody>(); foreach (var child in rigidbodys) { print("子物体名:"+child.name); }