Error in Camera

When I try to compile, the compiler will give me the error of “Cannot not implicitly convert type ‘UnityEngine.Camera[]’ to ‘UnityEngine.Camera’” for:
Camera camera = GetComponentsInChildren();

CameraRotation.cs and BallLauncher.cs are both attached to Player. And Main Camera is under Player. And I compared both of the scripts to this point and they are identical.

One other thing I noticed, is when I move Player around in the scene, the Main camera does not seem to move together with the player. So everytime I have to go back and make sure the camera is on the same position as the player.

GetComponentsInChildren(), because “components” is plural, can be expected to return a collection of components, in this case an array Camera[], instead of a single Camera instance.

Camera camera = GetComponentsInChildren()[0];

would work, if the first component among the children is the camera you want.

Another possibility–maybe it should be

Camera camera = GetComponentInChildren();

without the “s” on Components. That would get the first one.

1 Like

Privacy & Terms