How to use GUIUtility.hotControl

I’m trying to prevent mouse clicks from going over the UI and detecting what is behind (I guess technically I want to disable raycasting when the mouse is over the GUI).

I looked at this answer and tried to use the GUIUtility.hotControl but I cant figure out how it works…

My GUI is a small box that pops right in front of the selected object (kind of old point and click games). Here is my current UI Code

function OnGUI () {

	openOrClosed = playerNavigation.objContainerProperties.containerIsOpen; =  customSkin;
	GUI.depth = 0;

	if (playerNavigation.leftClickOnContainer == true && showGUIContainer == true){
		// Converting the selectedObject location to ScreenPoint
		var objScreenPoint = camera.main.WorldToScreenPoint(playerNavigation.selectedContainer.position);
		Debug.Log("objScreenPoint is: " + objScreenPoint);	
		// defining the values for X and Y for the UI using the selected Object ScreenPoint (so that the UI spawns where the object is)
		var mouseX = objScreenPoint.x;
		var mouseY = Screen.height - objScreenPoint.y;

		// Defines the Group that contains the GUI
		GUI.BeginGroup (Rect (mouseX - 90, mouseY -40, 180,160), "");
			// Frame around the gui with the object name.
			GUI.Box (Rect (0, 0, 180, 160), playerNavigation.objContainerProperties.objName);
			// LookAt
			if (GUI.Button (Rect (0, 60, 180, 32), "LOOK AT")) {
				objInteractions.LookAtContainer ();
				showGUIContainer = false;
			// Open/Close
			if (openOrClosed == false){
				if(GUI.Button (Rect (0, 90, 180, 32), "OPEN")) {		
					objInteractions.OpenContainer ();
					showGUIContainer = false;
			else if (openOrClosed == true){
				if(GUI.Button (Rect (0, 90, 180, 32), "CLOSE")) {		
					objInteractions.CloseContainer ();
					showGUIContainer = false;
			// Look Inside
			if (GUI.Button (Rect (0, 120, 180, 32), "LOOK INSIDE")) {
				//Application.LoadLevel (2);
		GUI.EndGroup ();

Any help would be really appreciated! I’ve been going round in circles trying to figure this out…

From unity script reference, GUIUtility.hotControl:

once the user mouseup’s, the control sets hotControl to 0

This means that when mouse clicks a button, hotControl is not equal to 0. You simply need to check if hotControl is equal to 0 or not before your raycasting/mouse input code.

Simple example:

void Update()
if (GUIUtility.hotControl == 0)

However, if I recall correctly, hotControl is set to the controlID of the button only on a mousedown. Mouse over does not trigger it.

If you want to disable your raycasting when mouse cursor is over a button, you need to detect the mouseover yourself, which can be done using Rect’s non-static method “Contains”.

if that do not works for you, try restricting Input.GetMouseButtonDown.position