transfrom.Find(string name)
找到一个名字为name的子物体,并返回他。
找到的只能是可见子物体,若不存在该名字的子物体,返回空;若存在多个该名字(同名)的子物体,返回其中一个,当返回的子物体被销毁,返回另一个该名字子物体。
GameObject.Find(string name)
找到一个名字为name的物体,并返回他。
可找到可见的所有物体,若不存在该名字的物体,返回空;若存在多个该名字的物体,返回其中一个,当返回的物体被销毁了,返回另一个 该名字的物体。
脚本挂载到父物体上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChildAndFather : MonoBehaviour
{
public string sonName;
public string objName;
// Update is called once per frame
void Update()
{
transform.Find(sonName).Rotate(new Vector3(1, 0, 0));
GameObject.Find(objName).transform.Rotate(new Vector3(0, 1, 0));
GameObject.Find(sonName).transform.Rotate(new Vector3(0, 1, 0));
}
}
物体层级,脚本挂载在father物体上
运行效果
存在多个同名子物体,transform.Find()找到一个子物体,GameObject也是一样,找到一个。当删除返回的物体时,会返回新的物体。