Question by
Dwakers92 · Feb 25, 2019 at 02:32 PM ·
c#guiraycastif statementgetmousebuttondown
I am trying to close the GUI if its already active and the user clicks
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1000))
// adjust the hit distance for the raycast distance to load the GUI
{
FloorGUI.SetActive(true);
Debug.Log("FLOOR CLICKED - LOADING GUI FOR CUSTOMER SELECTION");
}
else
{
Debug.Log("FLOOR NOT CLOSE ENOUGH FOR RAYCAST - WILL NOT LOAD GUI FOR CUSTOMER SELECTION");
}
if (Input.GetMouseButtonDown(0))
FloorGUI isActiveAndEnabled
FloorGUI.SetActive(false);
Debug.Log("CLOSING GUI, FLOOR HAS BEEN RE-CLICKED WITH GUI ACTIVE");
}
}
}
Comment
Best Answer
Answer by xxmariofer · Feb 25, 2019 at 02:52 PM
test thid code
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1000))
// adjust the hit distance for the raycast distance to load the GUI
{
FloorGUI.SetActive(!FloorGUI.activeSelf);
}
else
{
Debug.Log("FLOOR NOT CLOSE ENOUGH FOR RAYCAST - WILL NOT LOAD GUI FOR CUSTOMER SELECTION");
}
}
}
Answer by Dwakers92 · Feb 25, 2019 at 11:24 PM
That worked brilliantly! Thank you very much.
So i learn, may i ask what exactly is the difference in code? And how does it work this way?
you were setting the FloorGUI active and a bit lower will set it to false again. with the code i provided you only set it once to true or tu false depending on his current state.
Your answer
![](https://koobas.hobune.stream/wayback/20220612192436im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
UI panel without Image component as Raycast target? It is possible? 5 Answers
Problem with UI GraphicRaycasting 1 Answer
I am trying to close the GUI if its already active and the user clicks 0 Answers
2D Object effect like Life is Strange? 0 Answers
How to check if a 2D object is ahead of the player? 1 Answer