I am trying to use a raycast that writes the name of the object that hits and the position and when shooting the ray it gives a null reference exception. It happens at this line Ray ray = cam.ScreenPointToRay(Input.mousePosition); Here is the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
Camera cam;
// Start is called before the first frame update
void Start()
{
cam = Camera.current;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
//a ray is created
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//it is then casted
if (Physics.Raycast(ray, out hit))
{
Debug.Log("We have hit " + hit.collider.name + " " + hit.point);
}
}
}
}