I have a script that essentially is the raycast for a button, but i want a special object topop in and out that is defined by another script and it keeps giving me the error, i know what it means, it means that there is no value in the hit of the raycast so it can’t do the function, but i don’t know how to fix it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FPbutton : MonoBehaviour {
public Camera playerCam;
public Transform playerCam1;
public GameObject hand;
// Use this for initialization
void Start () {
hand.gameObject.SetActive(false);
}
// Update is called once per frame
void Update () {
RaycastHit hit;
Ray ray = playerCam.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray.origin, ray.direction, out hit, 2))
{
Remotedoor remotedoor = hit.transform.GetComponent<Remotedoor>();
if (remotedoor)
{
remotedoor.pressed = true;
Debug.Log("press");
}
}
}
if (Physics.Raycast(ray.origin, ray.direction, out hit, Mathf.Infinity))
{
DetectScreen screen = hit.transform.GetComponent<DetectScreen>();
if (Physics.Raycast(ray.origin, ray.direction, out hit, 2))
{
Remotedoor remotedoor = hit.transform.GetComponent<Remotedoor>();
if (remotedoor)
{
if (screen)
{
screen.active = true;
}
}
if (Physics.Raycast(ray.origin, ray.direction, out hit, Mathf.Infinity))
{
DetectScreen screen1 = hit.transform.GetComponent<DetectScreen>();
if (!screen)
{
screen1.active = false;
}
else
{
return;
}
}
}
}
}
}