1.添加tag
GameObject obj = GameObject.FindWithTag("Player");
仅适用于tag场景中只有一个player tag时,若场景中有多个player tag,也只返回一个(场景中你最后创建的那个游戏物体)
注意:无论运行该函数的游戏物体处于哪个父子层级,也无论目标tag处于哪个父子层级均能正常找到(遍历所有游戏物体)
2.GameObject.FindGameObjectsWithTag
GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject obj in objs)
{
Debug.Log(obj.name);
}
查找所有目标tag的游戏物体
3.transform.find
Transform t = transform.Find("3");
Debug.Log(t.name);
注意:该函数只能查找运行该函数的游戏物体下的子物体