我试图建立一个系统,使它能够分析一个给定的父母的所有子女,并根据其中一个活跃的子女,决定该子女的前进和权利是什么。
Vector3 camF = new Vector3(0,0,0);
Vector3 camR = new Vector3(0,0,0);
//Assign active camera to current cam.
GameObject[] cameras = GetComponentsInChildren<GameObject>();
foreach (GameObject currentCam in cameras)
{
if (currentCam.activeInHierarchy)
{
camF = currentCam.transform.forward;
camR = currentCam.transform.right;
}
}
当游戏启动时,我无法移动我的角色,并一直收到这个错误信息。
ArgumentException: GetComponent requires that the requested component 'GameObject' derives from MonoBehaviour or Component or is an interface.
UnityEngine.GameObject.GetComponentsInChildren[T] (System.Boolean includeInactive) (at <58a34b0a618d424bb5fc18bb9bcdac20>:0)
UnityEngine.Component.GetComponentsInChildren[T] (System.Boolean includeInactive) (at <58a34b0a618d424bb5fc18bb9bcdac20>:0)
UnityEngine.Component.GetComponentsInChildren[T] () (at <58a34b0a618d424bb5fc18bb9bcdac20>:0)
PerspectiveControls.Update () (at Assets/Scripts/Character/PerspectiveControls.cs:32)
问题是你使用的是 GetComponent
而 GameObject
不是一个组件。另一方面,Transform包含了所有的子程序。所以,为了得到子代Transform,你可以使用这个。
foreach (Transform child in transform)
而为了到达GameObject,你可以这样做:
child.gameObject