I have this script that when a raycast points at a door and i hit E i enter the door. The script works but everytime my camera doesn’t point at a door i get a nullreference exception.
// Cast a ray to see what the camera is looking at.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) ;
{
// Find the door associated with the hit collider.
GameObject doorObj = GameObjectUtils.FindObjectWithTagUpHeirarchy(hit.collider.gameObject, "Door");
if (doorObj) ;
{
if (hit.collider == doorObj) ;
{
var doorComponent = doorObj.GetComponent<DoorComponent>();
interactTextObj.SetActive(true);
if (doorComponent.leadsToAnotherCell)
{
interactTextObj.GetComponent<Text>().text = doorComponent.doorExitName;
}
else
{
interactTextObj.GetComponent<Text>().text = doorComponent.doorName;
}
if (Input.GetKeyDown(KeyCode.E))
{
OpenDoor(doorComponent);
}
}
}
}