0

I'm trying to make a instance of a GameObject at the position of the mouse when LeftMouseButton is pressed ,im trying to convert it to world position but i get a error : Member 'Camera.main.current' cannot be accessed with an instance reference; qualify it with a type name instead

public GameObject tower;  
   public Camera sceneCamera;
   Vector3 objectP;   
    void Update () 
     {
        sceneCamera = Camera.main;
        if (sceneCamera!= null) 
        {
            Vector3 mousePos = Input.mousePosition;
         mousePos.z = 2.0f;
        Vector3 objectP = Camera.main.current.ScreenToWorldPoint(mousePos);
        }
         
     if (Input.GetButtonDown("Fire1")) 
         {
         Instantiate(tower,objectP, Quaternion.identity);
         }
     }
    
   
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • `Camera.current` is a static reference to the current camera. Instead use `Camera.main` which is the instance of the main camera. You get the error because you are trying to access a static property of the type Camera, which cannot be accessed using an instance of a camera. [Camera.current](https://docs.unity3d.com/ScriptReference/Camera-current.html) vs [Camera.main](https://docs.unity3d.com/ScriptReference/Camera-main.html) – hijinxbassist Aug 25 '21 at 19:20
  • Via [`Camera.main` documentation](https://docs.unity3d.com/ScriptReference/Camera-main.html) and [`ScreenToWorldPoint()` documentation](https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html): `Camera.main.current.ScreenToWorldPoint(mousePos)` should be `Camera.main.ScreenToWorldPoint(mousePos)`. – D M Aug 25 '21 at 19:23
  • 2
    Does this answer your question? [Member '' cannot be accessed with an instance reference](https://stackoverflow.com/q/1100009/1092820) – Ruzihm Aug 25 '21 at 19:33

1 Answers1

1

Replace Camera.main.current with Camera.main.

main and current are both static properties of the Camera class, so it doesn't make sense to chain them like you're doing (and is, in fact, disallowed).

Spencer Bench
  • 647
  • 4
  • 7