MouseOver Detection

How would I detect if my mouse is over a certain GUI element? Thanks for the help.

There are several ways:

  1. Using a GUITexture, See the function OnMouseOver(). With GUITextures, you can also use GUITexture.HitTest() with the mousePos to return true or false.

  2. Using OnGUI, The GUI will automatically react to the user putting the mouse over it. Change the style for "Over" in your GUI Style.

  3. You could find the Rect the GUI element then use Rect.Contains() with the mouse position to find if the mouse overlaps with the same position as the GUI. This would probably be the easiest way to find it if the object that needs to know is not the same object as the GUI.

I’ve found another way of doing this. Basically my UI is all drawn using a single script, so rather than checking each widget for Rect.Contains, I use a camera oriented Quad that covers the whole viewport and follows the camera but is rendered with ZWrite Off and ColorMask 0, so basically it is invisible and do not modify the depth buffer. Then on that Quad i override OnMouseHover(). Basically if the mouse is over any GUI.Button(), scrollview etc
OnMouseHover() will not trigger, if OnMouseHover() triggers, then you know the mouse is not interacting with any GUI, and you can do your object rotation/highlight etc. This way regardless of how many GUI’s you have on screen you do not need extra test code per widget.

The UI script is set to execute first btw.

static function IsMouseOver() : boolean
return Event.current.type == EventType.Repaint &&

function OnGUI()
    if (GUILayout.Button("Click"))
    if (IsMouseOver())
        Debug.Log("mouse is over just shown button");

Use GUI.tooltip

var mouseover : String;

function OnGUI(){

GUI.Button(Rect(10,10,100,30), GUIContent("Button1", "MouseOverOnButton1")); mouseover = GUI.tooltip; }

function Update(){

if(mouseover == "MouseOverOnButton1"){

print("Hovering Button1"); } }

Or check the screen position of the Mouse with Input.mousePosition.x /.y .

if(Input.mousePosition.x < 50 && Input.mousePosition.y < 50){