- Home /
Why Does IsPointerOverGameObject Not Work with GUILayout Controls?
I have a game where holding the left mouse button makes the player float up into the air.
I've just added a GUI into the game.
I want to use an "Automatic Layout" using GUILayout, instead of a "Fixed Layout" using GUI.
This is because the "Automatic Layout" using GUILayout automatically sizes the GUI.
If I use a "Fixed Layout" using GUI, then I can use IsPointerOverGameObject to check if the mouse is over the GUI when clicking a control, so that the player will not float up into the air.
But if I use an "Automatic Layout" using GUILayout, IsPointerOverGameObject doesn't work, and the player will still float up into the air when clicking a control.
So why doesn't IsPointerOverGameObject work with the GUILayout's "Automatic Layout"?
And how can I achieve the same effect using GUILayout's "Automatic Layout"?
Example of working code for a "Fixed Layout" using GUI:
if (Input.GetMouseButton(0))
{
if (EventSystem.current.IsPointerOverGameObject()) {return;} // Detects clicks on the UI.
rb.AddForce(Vector3.up * speed); // Thrust
}
Your answer
Follow this Question
Related Questions
How to position a GUI-Element relative to a game object? 0 Answers
Similar function in UI for GUILayoutUtility.GetRect 0 Answers
How do you highlight/select a button upon mouse enter and keep it moused over upon mouse exit? -2 Answers
Make children of a Layout Group fit their respective sizes (confusing docs) 1 Answer
Help with GUILayout and tooltip(C#) 1 Answer