直接上代碼吧,廢話不多說。
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()是找不到應經隱藏的的物體的。