直接上代码吧,废话不多说。
using UnityEngine; using System.Collections; public class FindChildObject : MonoBehaviour { //要找子物体的那个物体 public Transform parent; //想找的子物体的名字 public string childName; // Use this for initialization void Start () { GetTransform(parent, childName); } // Update is called once per frame void Update () { } Transform GetTransform(Transform check, string name) { foreach (Transform t in check.GetComponentsInChildren<Transform>()) { if (t.name == name) { //要做的事 Debug.Log(t.name); return t; } } return null; } }
当然,你还可以直接就用transform.Find()。方法去找。
注意:transform.Find()是可以找到隐藏的物体的,但是GameObject.Find()是找不到应经隐藏的的物体的。