Hello, I am trying to make a button script, aka you look at an object and it shows you a UI saying “press “Key” to whatever” and then when you press said button it makes you hold it like your doing a task and then when it’s done it adds a point for an example. For this system, I am using raycasts which means I’m using two scripts, the raycast caster, and receiver and I have gotten stuck on the raycast situation. I am trying to use a bool to check if the raycast is hitting the “button” or is not so I can pull up the Ui, but it is giving me an error saying I can’t do such a thing.
So I came here to ask for one of two things.
To get a video or a self-made tutorial (of course, if you want and only if you want.) of how to do what I am doing in a more simple manner cause I am using what feels like 100 voids to make it work.
Or to get help looking over my code because I am newish to coding and I have no solution for it.
Thank you, code is below:
//raycast sender
using UnityEngine;
public class taskRaycast : MonoBehaviour
{
public bool raying = false;
public Camera cam;
void Update()
{
raycaster();
}
void raycaster()
{
RaycastHit hit;
if(Physics.Raycast(cam.transform.position, cam.transform.forward, out hit))
{
Debug.Log(hit.transform.name);
taskTarget target = hit.transform.GetComponent<taskTarget>();
if(target != null)
{
target.CheckRaying();
}
}
}
}
//raycast receiver
using UnityEngine;
public class taskTarget : MonoBehaviour
{
public GameObject OpenPanel = null;
bool getRaycast;
public void showTask()
{
OpenPanel.SetActive(true);
}
public void hideTask()
{
OpenPanel.SetActive(false);
}
private bool IsOpenPanelActive
{
get
{
return OpenPanel.activeInHierarchy;
}
}
void Update()
{
if(IsOpenPanelActive)
{
if(Input.GetKeyDown(KeyCode.E))
{
OpenPanel.SetActive(false);
return;
}
}
if(getRaycast = true)
{
showTask();
}
else{
hideTask();
}
}
public void CheckRaying(bool statement)
{
if(IsOpenPanelActive)
{
getRaycast == statement;
}
else
{
getRaycast == statement;
}
}
}