# UnityGUI click event

**URL:** https://discussions.unity.com/t/unitygui-click-event/21707
**Category:** Questions & Answers
**Created:** [May 24, 2011, 9:34am UTC](https://discussions.unity.com/t/unitygui-click-event/21707 "2011-05-24T09:34:07Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![anon38624328](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@anon38624328](https://discussions.unity.com/u/anon38624328)
#### Post date: [May 24, 2011, 9:34am UTC](https://discussions.unity.com/t/unitygui-click-event/21707/1 "2011-05-24T09:34:07Z")

</div>

I’ve created a UI using UnityGUI.  
When I click on either GUI.Button or GUI.Box the click is processed by objects on the scene.

```
if (Input.GetKeyDown ("mouse 0")) {
    Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);			
    if (Physics.Raycast (ray, out hit)) {
    // ...
    }
}

```

I need to determine the fact, that the UI was clicked on.  
In case of GUI.Button I can check wether GUIUtility.hotControl != 0.  
GUI.Box doesn’t modify GUIUtility.hotControl.

What is the common solution?

---

<div class="post-metadata">

### Author: ![FadeToBlack](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@FadeToBlack](https://discussions.unity.com/u/FadeToBlack)
#### Post date: [June 3, 2012, 10:23am UTC](https://discussions.unity.com/t/unitygui-click-event/21707/4 "2012-06-03T10:23:52Z")

</div>

just now i found the next solution (i use it in dragging camera routine):

```
		if (Input.GetMouseButtonDown(2) && GUIUtility.hotControl == 0) {
		pos = cursor.getPos(false);
		drag = true;
	}

```

this condition ignores mouse down when we click on gui elements

---

<div class="post-metadata">

### Author: ![Bunny83](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@Bunny83](https://discussions.unity.com/u/Bunny83)
#### Post date: [May 24, 2011, 10:59am UTC](https://discussions.unity.com/t/unitygui-click-event/21707/2 "2011-05-24T10:59:27Z")

</div>

Ahhh, finally i got your problem :D. To sum up:

- You have a GUI on your screen
- You use the above posted raycast to interact with GameObjects in your scene
- You don’t want to interact with objects that are behind the GUI when you click on the GUI.

I think that’s your problem, right? It’s really hard to get that out of your question.

You can simply check if the mouse is inside the Rect of your box/window. If you use [GUI.Box](http://unity3d.com/support/documentation/ScriptReference/GUI.Box.html) just use the Rect you’re using to create the Box. If you use GUILayout you can use [GUILayoutUtility.GetLastRect](http://unity3d.com/support/documentation/ScriptReference/GUILayoutUtility.GetLastRect.html) to determine the Rect. To check if you are over the GUI use [Rect.Contains](http://unity3d.com/support/documentation/ScriptReference/Rect.Contains.html) with the mouse position (inside OnGUI use [Event.current.mousePosition](http://unity3d.com/support/documentation/ScriptReference/Event-mousePosition.html)).

```
//C#
bool overGUI = false;
void OnGUI()
{
    Rect guiRect = new Rect(10,10,100,100);
    GUI.Box(guiRect);
    if(GUI.Button(new Rect(20,20,80,30),"Click Me"))
    {
        // clicked
    }
    if (Event.current != null && Event.current.isMouse)
    {
        overGUI = guiRect.Contains(Event.current.mousePosition);
    }
}

```

or with GUILayout

```
bool overGUI = false;
void OnGUI()
{
    GUILayout.BeginVertical("", "Box"); // style it like a Box
    if(GUILayout.Button("Click Me"))
    {
        // clicked
    }
    GUILayout.EndVertical();
    Rect guiRect = GUILayoutUtility.GetLastRect();
    if (Event.current != null && Event.current.isMouse)
    {
        overGUI = guiRect.Contains(Event.current.mousePosition);
    }
}

```

Now you can check the boolean if your over the GUI or not 😉

ps. I wrote that from scratch so it’s not checked for syntax errors 😉

---

<div class="post-metadata">

### Author: ![Owen-Reynolds](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/owen-reynolds/32/347105_2.png) [@Owen-Reynolds](https://discussions.unity.com/u/Owen-Reynolds)
#### Post date: [February 10, 2012, 9:59pm UTC](https://discussions.unity.com/t/unitygui-click-event/21707/3 "2012-02-10T21:59:16Z")

</div>

This is what worked for me. It ignores the first and all clicks in a GUI.textField, and sees even the very first click outside of one:

```
if(Input.GetMouseButtonDown(0) && GUI.GetNameOfFocusedControl()=="") {...}
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

```

Turns out the rule about not using GUI stuff except in OnGUI doesn’t apply to getNoFC. It _can_ be examined from Update. You have to be sure to set names – ex: `GUI.SetNextControlName("b");` – in front of all your focusable controls (you can be lazy and give them all the same name.)

---

<div class="post-metadata">

### Author: ![arlinon](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@arlinon](https://discussions.unity.com/u/arlinon)
#### Post date: [December 29, 2013, 2:48pm UTC](https://discussions.unity.com/t/unitygui-click-event/21707/5 "2013-12-29T14:48:24Z")

</div>

I tried to use above methods and few other but couldn’t get what I really want.

I ended up making a rect for where I see the scene from and raycast through. Game just checks whether the scene rect contains mouse click as explained in here [Unity - Scripting API: Rect.Contains](http://docs.unity3d.com/Documentation/ScriptReference/Rect.Contains.html)

---

<div class="post-metadata">

### Author: ![vikingfabian-com](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/vikingfabian-com/32/126675_2.png) [@vikingfabian-com](https://discussions.unity.com/u/vikingfabian-com)
#### Post date: [August 28, 2014, 12:37pm UTC](https://discussions.unity.com/t/unitygui-click-event/21707/6 "2014-08-28T12:37:25Z")

</div>

This works for editor GUI

```
public static bool LeftMouseUp()
{
    return Event.current != null && Event.current.type == EventType.MouseUp && Event.current.button == 0;
}

```
