- Home /
unable to turn enntity on/off with raycast
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;
}
}
}
}
}
}
$$anonymous$$ay I ask why you have so many nested Raycasts?
Answer by Trevdevs · Jun 24, 2018 at 01:37 AM
Like Vicarian said your nested raycast is highly uncessary looks like to me your code is not running because you have your screen reversed
what your saying here is if(screen == true) set it == true? that is the opposite of what you want to do here is some simplified code for ya and for the record tags
void Update () {
RaycastHit hit;
Ray ray = playerCam.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray.origin, ray.direction, out hit, 2))
{
if (hit.transform.GetComponent<Remotedoor>() != null)
{
Remotedoor remotedoor = hit.transform.GetComponent<Remotedoor>();
remotedoor.pressed = true;
Debug.Log("press");
}
else if(hit.transform.GetComponent<DetectScreen>() != null)
{
DetectScreen screen = hit.transform.GetComponent<DetectScreen>();
screen.gameObject.SetActive(false); //wasnt sure what you were trying to do with the screen so I put this here
}
}
}
And just a small note your not follow OOP program$$anonymous$$g appropriately I'm just a stickler about this what you did wasn't necessarily wrong lol.
.GetComponent doesnt always have to be assigned it actually can return a bool if the object finds that component or not so what I changed it to do was
//What I did If I find this component on this object create a reference to this object.
//What you technically did Create a reference to a object that might exist, check if it really does it exist