- Home /
RayCast Activating A Button
So I followed a brackeys tutorial as I'm new to this type of thing. I am attempting to write a script that will activate a button when you press it. I'm using a raycast that checks for the button and then activates it. When activated, the button is supposed to print "Hello World" into the console. However, it does not do this. Thanks in advance!
This is the player that activates the button:
public float range = 5f;
public Camera FPSCam;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Activate();
}
}
void Activate()
{
RaycastHit HitInfo;
if (Physics.Raycast(FPSCam.transform.position, FPSCam.transform.forward, out HitInfo, range))
{
ButtonPress Button = HitInfo.transform.GetComponent<ButtonPress>();
if (Button != null)
{
Button.active = true;
}
}
}
And this is the script for the button itself:
public bool active = false;
public void Activate()
{
if (active == true)
{
Debug.Log("Hello World");
}
}
Comment
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How to draw a line out which traces the SweepTest ray? 2 Answers
Raycast hits multiple targets 1 Answer
Multiple Cars not working 1 Answer
Why won't my function show up in the onClick area of a button? 2 Answers