Legacy Documentation: Version 5.5
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

EventSystem.IsPointerOverGameObject

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public function IsPointerOverGameObject(): bool;
public bool IsPointerOverGameObject();
public function IsPointerOverGameObject(pointerId: int): bool;
public bool IsPointerOverGameObject(int pointerId);

Parameters

pointerId Pointer (touch / mouse) ID.

Description

Is the pointer with the given ID over an EventSystem object?

#pragma strict
public class MouseExample extends MonoBehaviour {
	function Update() {
		// Check if the left mouse button was clicked
		if (Input.GetMouseButtonDown(0)) {
			// Check if the mouse was clicked over a UI element
			if (EventSystem.current.IsPointerOverGameObject()) {
				Debug.Log("Clicked on the UI");
			}
		}
	}
}
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class MouseExample : MonoBehaviour {

void Update () { // Check if the left mouse button was clicked if(Input.GetMouseButtonDown(0)) { // Check if the mouse was clicked over a UI element if(EventSystem.current.IsPointerOverGameObject()) { Debug.Log("Clicked on the UI"); } } } }

If you use IsPointerOverGameObject() without a parameter, it points to the "left mouse button" (pointerId = -1); therefore when you use IsPointerOverGameObject for touch, you should consider passing a pointerId to it.

#pragma strict
public class TouchExample extends MonoBehaviour {
	function Update() {
		// Check if there is a touch
		if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {
			// Check if finger is over a UI element
			if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) {
				Debug.Log("Touched the UI");
			}
		}
	}
}
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class TouchExample : MonoBehaviour {

void Update () { // Check if there is a touch if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) { // Check if finger is over a UI element if(EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) { Debug.Log("Touched the UI"); } } } }

Note that for touch, IsPointerOverGameObject should be used with OnMouseDown() or Input.GetMouseButtonDown(0) or Input.GetTouch(0).phase == TouchPhase.Began.