The script has a variable: public Camera PlayerCam; Through the method public void Start() { ... } I want to assign the character’s main camera to this variable. Probyval with

 PlayerCam = GameObject.FindGameObjectWithTag("Player").transform.GetChild(0); 

But an error appears that a variable of type GameObject cannot be entered into a variable of type Camera.

    2 answers 2

    To take the main camera you can use

     PlayerCam = Camera.main; 

    And as stated above, you assigned the wrong data type to the camera. If you already find the camera object on the scene, then take the camera component from it and assign it to where you want.

     PlayerCam = GameObject.FindGameObjectWithTag("Player").transform.GetChild(0).gameObject.GetComponent<Camera>(); 

      View the GetChild () documentation — this method returns the Transform type, and you are trying to save it to a variable of type Camera . If you want to get any script attached to the GameObject you are looking for, then use the GetComponent method, specifying T - Camera as the parameter.

      It is worth noting that the code that you gave in the question is not very optimal and should not be used. It is best to link to components and objects in the Inspector.

      • In the future I am going to make the game network. As far as I know, it will not be possible to simply take and drag it all in the inspector. - JediMan4ik