using UnityEngine;
using System.Collections;
publicclass Communicate : MonoBehaviour {
// Use this for initializationvoid Start () {
Transform name1 = transform.Find ("GameObject/name");//使用transform.find只能查询自身下一级的子物体,且子物体是否激活都能查到,若查询多级的子物体则加'/',hierarchy的路径
Transform name2 = transform.FindChild("name");//这个跟transform.find一样
print(name1);
print(name2);
}
// Update is called once per framevoid Update () {
}
}
GameObject下面的
using UnityEngine;
using System.Collections;
publicclass Communicate : MonoBehaviour {
// Use this for initializationvoid Start () {
GameObject name1 = GameObject.Find("name");//能查询,hierarchy中所有的物体,但是必须是激活的
GameObject name2 = GameObject.FindGameObjectWithTag("name");//根据tag查找物体,但是必须是激活的
GameObject[] name3 = GameObject.FindGameObjectsWithTag("name");//根据tag查找同一tag的物体,但是必须是激活的
}
// Update is called once per framevoid Update () {
}
}
using UnityEngine;
using System.Collections;
publicclass Communicate : MonoBehaviour {
// Use this for initializationvoid Start () {
Transform name1 = FindObjectOfType(typeof(Transform))as Transform;//在hierarchy中查找有transform组件的物体,必须是激活的
Transform[] name = FindObjectsOfType(typeof(Transform))as Transform[];//在hierarchy中查找有transform组件的物体,必须是激活的
}
// Update is called once per framevoid Update () {
}
}