unity找到指定名称的一个物体的子物体,多个子物体有相同的名称

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Roation : MonoBehaviour {


	//要找子物体的那个物体
	private GameObject game;
	public Transform parent, tracker;
	//想找的子物体的名字
	public string childName;

	// Use this for initialization
	void Start () {
		childName = "Cube";
		game = GameObject.Find ("Plan01");

		tracker = GetTransform(game.transform, childName);
	}

	// Update is called once per frame
	void Update () {
		tracker.Rotate (new Vector3(0, 1, 0), 3);
		tracker.RotateAround (new Vector3(0, 0, 0), new Vector3(0, 1, 0), 1);

	}

	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;
	}


}

猜你喜欢

转载自blog.csdn.net/qq_21743659/article/details/125185497