Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Errors With Camera Definitions

Discussion in 'Scripting' started by Rudacious, May 9, 2022.

  1. Rudacious

    Rudacious

    Joined:
    Apr 28, 2022
    Posts:
    7
    I'm working through a tutorial on how to make a character shoot at the mouse position in a 2D game. The code below is what I have written so far, but visual studio is giving me an error that does not appear in the tutorial.

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Shooting : MonoBehaviour
    5. {
    6.     private Camera mainCam;
    7.     private Vector3 mousePos;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         mainCam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
    13.     }
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         mousePos = mainCam.ScreenToWorldPoint(Input.mousePosition);
    18.     }
    19. }
    The error is with the last line and reads:
    "CS1061: 'Camera' does not contain a definition for 'ScreenToWorldPoint' and no accessible extension method 'ScreenToWorldPoint' accepting a first argument of type 'Camera' could be found (are you missing a using directive or an assembly reference?)"

    I found a solution online that said instead to use the line:
    Code (csharp):
    1. mousePos = mainCam.main.ScreenToWorldPoint(Input.mousePosition);
    However, again the error reads:
    "CS1061: 'Camera' does not contain a definition for 'main' and no accessible extension method 'main' accepting a first argument of type 'Camera' could be found (are you missing a using directive or an assembly reference?)"

    Any advice helps, thank you.
     
    Last edited: May 9, 2022
  2. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    You might want to watch that tutorial again. It does not use:
    mousePos = mainCam.SetToWorldPoint(Input.mousePosition);

    It clearly shows in the tutorial as:
    mousePos = mainCam.ScreenToWorldPoint(Input.mousePosition);

    If you are going to follow a tutorial, then you really need to make sure you follow it exactly.
     
    DevDunk, Rudacious and Kurt-Dekker like this.
  3. Rudacious

    Rudacious

    Joined:
    Apr 28, 2022
    Posts:
    7
    Oops that was a typo only in my forum post. I will edit the post above, but the line I had in my code was ScreenToWorldPoint(). Good catch, however that wouldn't explain why mainCam.main wasn't working either.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    No, but the very nature of the C# language explains that exactly.

    Always start with the docs to understand any API you are trying to use:

    https://docs.unity3d.com/ScriptReference/Camera-main.html

    Camera.main
    is a static property... you can't use an instance variable to access it.

    You'll notice that horribly-long line 12 actually is what Camera.main does for you in the first place, so you're just replicating all of it yourself.
     
    Rudacious likes this.
  5. Rudacious

    Rudacious

    Joined:
    Apr 28, 2022
    Posts:
    7
    Thanks, however that still leaves me with the issue that I can't define Camera.main either. From
    Code (csharp):
    1. mainCam = Camera.main;
    I get the error: "CS0117: 'Camera does not contain a definition for 'main'"
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Did you name one of your scripts Camera?

    Don't do that.
     
    Rudacious likes this.
  7. Rudacious

    Rudacious

    Joined:
    Apr 28, 2022
    Posts:
    7
    Ah, in fact I did. Thank you Kurt!
     
    Kurt-Dekker likes this.