Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help: Camera' does not contain a definition for 'fieldOfView'

Discussion in 'Scripting' started by LoadedAK47, Apr 11, 2020.

  1. LoadedAK47

    LoadedAK47

    Joined:
    May 12, 2017
    Posts:
    2
    Code (CSharp):
    1. public class CameraControls : MonoBehaviour
    2. {
    3.     [Header("Camera Setting")]
    4.     [Tooltip("Camera pan speed")]
    5.     [SerializeField] float moveSpeed = 0.5f;
    6.     [Tooltip("Zoom speed")]
    7.     [SerializeField] float scrollSpeed = 1.0f;
    8.  
    9.     private float horizontalInput;
    10.     private float verticalInput;
    11.     private float wheelInput;
    12.  
    13.     [Header("Camera Pan Limit")]
    14.     [Tooltip("Camera pan limit, xyz")]
    15.     [SerializeField] Vector3 camLimit;
    16.  
    17.     [Header("Camera Zoom Limit")]
    18.     [Tooltip("Camera zoom limit, xy - forward, back")]
    19.     [SerializeField] Vector2 zoomLimit;
    20.  
    21.     private Camera cam;
    22.  
    23.     private void Awake()
    24.     {
    25.         cam = GetComponent<Camera>();
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         horizontalInput = Input.GetAxisRaw("Horizontal");
    31.         verticalInput = Input.GetAxisRaw("Vertical");
    32.         wheelInput = Input.GetAxis("Mouse ScrollWheel");
    33.  
    34.         if (horizontalInput != 0 || verticalInput != 0)
    35.         {
    36.             transform.position += moveSpeed * new Vector3(horizontalInput, 0, verticalInput);
    37.         }
    38.         /*
    39.         if (Input.GetAxis("Mouse ScrollWheel") != 0)
    40.         {
    41.             transform.position += scrollSpeed * new Vector3(0, wheelInput, 0);
    42.         }
    43.         */
    44.         if (Input.GetAxis("Mouse ScrollWheel") != 0)
    45.         {
    46.             cam.fieldOfView = Mathf.Clamp(cam.fieldOfView * scrollSpeed , zoomLimit.x , zoomLimit.y);
    47.         }
    48.  
    49.         transform.position = new Vector3
    50.                             (
    51.                                 Mathf.Clamp(transform.position.x, -camLimit.x, camLimit.x),
    52.                                 Mathf.Clamp(transform.position.y, -camLimit.y, camLimit.y),
    53.                                 Mathf.Clamp(transform.position.z, -camLimit.z, camLimit.z)
    54.                             );
    55.     }
    56. }
    So I was testing some basic camera movement and zooming and the code compiled just fine. My comp locked up and when it came back it started shooting me this error,
    "error CS1061: 'Camera' does not contain a definition for 'fieldOfView' and no accessible extension method 'fieldOfView' accepting a first argument of type 'Camera' could be found (are you missing a using directive or an assembly reference?)"

    I'm at a loss. I go to the api and copy the code
    Code (CSharp):
    1.  
    2.    void Start()
    3.    {
    4.        //Start the [URL='https://docs.unity3d.com/ScriptReference/Camera.html']Camera[/URL] field of view at 60
    5.        m_FieldOfView = 60.0f;
    6.    }
    7.  
    8.     void [URL='https://docs.unity3d.com/ScriptReference/PlayerLoop.Update.html']Update[/URL]()
    9.    {
    10.        //[URL='https://docs.unity3d.com/ScriptReference/PlayerLoop.Update.html']Update[/URL] the camera's field of view to be the variable returning from the [URL='https://docs.unity3d.com/ScriptReference/UIElements.Slider.html']Slider[/URL]
    11.        Camera.main.fieldOfView = m_FieldOfView;
    12.    }
    13.  
    and it still gives me the same error. I even made an entirely new script and same. Can someone help me, I feel like it something dumb, did my stuff get corrupted? thank you in advance.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    What do the imports in your file look like? Do you have any custom scripts that define a class called Camera?
     
  3. Alvarezmd90

    Alvarezmd90

    Joined:
    Jul 21, 2016
    Posts:
    149
    I don't recommend using Camera.main.fieldOfView. That literally does
    Code (CSharp):
    1. GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().fieldOfView;
    The refer probably fails because it didn't find the tag on it.
     
  4. LoadedAK47

    LoadedAK47

    Joined:
    May 12, 2017
    Posts:
    2
    @PreatorBlue, omg, it appears there was another script with a class defined Camera. So that is the culprit, not sure where it came from, must've been an import.... Thankyou, that fixed the issue!!!

    @Alvarezmd90, I agree, I don't use that line of code. I just copied that from the unity API for testing purposes.
    Code (CSharp):
    1.  
    2.   private Camera cam;
    3.  
    4.     private void Awake()
    5.     {
    6.         cam = GetComponent<Camera>();
    7.     }
    8.  
    9.     void Update()
    10.     {
    11.         if (Input.GetAxis("Mouse ScrollWheel") != 0)
    12.         {
    13.             cam.fieldOfView = Mathf.Clamp(cam.fieldOfView * scrollSpeed , zoomLimit.x , zoomLimit.y);
    14.         }
    15.     }
    16.  
    is what I used. good point out either way
     
    Alvarezmd90 likes this.
  5. KiraSof

    KiraSof

    Joined:
    Apr 2, 2021
    Posts:
    1
    Someone posted this and he was right, IT FIXED MINE
    ''I had the same error. As it turns out, if one of your scripts is called Camera, it looks for Camera.main inside that script, instead of using the built-in Camera class. So either rename your Camera script, or add the UnityEngine namespace in front of your call:

    1. UnityEngine.Camera.main.WorldToScreenPoint(transform.position);''
     
    Gaurav27 likes this.
  6. luizhenrqueps740

    luizhenrqueps740

    Joined:
    Apr 28, 2021
    Posts:
    1
    [QUOTE = "KiraSof, post: 7415978, membro: 7131122"] Alguém postou isso e ele estava certo, IT FIXED MINE
    ''Eu tive o mesmo erro. Acontece que, se um de seus scripts for chamado Camera, ele procurará Camera.main dentro desse script, em vez de usar a classe interna Camera. Portanto, renomeie o script da câmera ou adicione o namespace UnityEngine na frente da sua chamada:

    1. UnityEngine.Camera.main.WorldToScreenPoint (transform.position); ''
    [/CITAR]
    thank you brazil