Camera.main.ScreenToWorldPoint() Not Working

I have a 3D game and I am trying to make it so that it spawns a gameobject where I click with cam.ScreenToWorldPoint(mousePosition), but whenever I click it just inserts the game object at the position of the camera? Why is this and how can I fix it?

void Update()
    {
        if (Input.GetKeyDown("mouse 0"))
        {
            Vector3 point = new Vector3();


            Vector3 mousePos = Input.mousePosition;

            point = Camera.main.ScreenToWorldPoint(mousePos);

            Instantiate(g, point, Quaternion.identity);
        }
    }

It’s counter-intuitive, but you need to set the Z position as the distance from the camera.

Camera cam = Camera.main;

Vector3 mousePos = Input.mousePosition;
mousePos.z = cam.nearClipPlane;

point = cam.ScreenToWorldPoint(mousePos);

For a full 3D game with a rotatable camera, you’ll probably want to use a Raycast eventually anyways.

Didn’t you want something like this?

private void Update()
{
   if(!Input.GetMouseButtonDown(0)) return;
   var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
   if (Physics.Raycast(ray, out var hit)) {
      Instantiate(g, hit.transform.position, Quaternion.identity);
   }
}

Thank you, this helped a lot.