Reputation: 67
I tried to make a system so that it would analyse all of the children of a given parent and based on one of the children whom is active, decide upon what the forward and right of that child is:
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;
}
}
While the game does boot, I cannot move my character, and keep on getting this error message:
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)
Upvotes: 0
Views: 8178
Reputation: 512
The problem is you're using GetComponent
method but GameObject
is not a component. On the other hand, transform contains all the children. So, in order to get children Transforms, you can use this:
foreach (Transform child in transform)
And in order to reach the GameObject, you can do this:
child.gameObject
Upvotes: 3