# Clicking pause button makes character jump before pausing

I have an issue with my Unity 5 game. When I click on the Pause button, my character jumps before the game is paused.

My script detects the click to trigger the jump jump before the pause button. I tried adding a tag check but that didn't work:

void Update () {
if (Input.GetKeyDown (KeyCode.Space) && Time.timeScale == 1 || Input.GetMouseButtonDown (0) && Time.timeScale == 1 && gameObject.tag != "Pause") {
}


You can see in this gif, when I click on the pause button it jumps and pauses directly.

How can I click on the Pause button without making my player jump?

• Well I deleted my answer because I didn't understand the problem. Hopefully someone else comes along with an answer. Sep 28 '15 at 1:31
• why the vote down? Sep 28 '15 at 2:45
• gameObject.tag refers to the tags on this.gameObject, ie. your bird. It does not refer to the tags on the object that has been clicked. Also, watch out for doing GetComponent<> every frame. It's usually better to do that once in Start, and then cache the reference in a member variable to use later. Sep 28 '15 at 4:21

I had the same issue when I was doing a game. What basically happens is that your script detects if there is a mouse click, including when you click on UI components.

There is a solution to that fortunately:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class PlayerControl : MonoBehaviour {

void Update () {
if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject(0))
{
MovePlayer();
}
}
}


So now every-time you tap on a screen unity checks if there is no UI element (buttons, panels etc.) where you tapped before moving your player.

Hope this helps! :)

• It doesn't work Sep 28 '15 at 13:52
• Can you clarify what's happening on the screen right now? Sep 29 '15 at 10:34
• When I write your script, nothing change just like I didn't do anything Sep 29 '15 at 14:33

Ok, the .gif makes it make more sense. I think this is what you're looking for:

void Update () {
Vector2 buttonDimensions = new Vector2(50, 50);
Rect pauseButtonPosition = new Rect(Screen.width - buttonDimensions.x, 0, buttonDimensions.x, buttonDimensions.y);

bool mouseOnPauseButton = pauseButtonPosition.Contains(Input.mousePosition);

if (!mouseOnPauseButton && Input.GetKeyDown(KeyCode.Space) && Time.timeScale == 1 || Input.GetMouseButtonDown(0) && Time.timeScale == 1 && gameObject.tag != "Pause") {
}
}


So basically if the mouse is on the pause button when you try to jump, it wont.

• It still doesn't work Sep 28 '15 at 13:52
• You'll have to adjust the pause button position manually. If this doesn't work, I have to say that you're issue is somewhere else. Sep 28 '15 at 23:45
• Thank you I change it manually and it works, and also is it good for performance to remove Vector2 buttonDimensions = new Vector2(50, 50); and just write the position and dimensions in Rect Sep 29 '15 at 0:48
• Technically yes, but the difference would be so minuscule that it wouldn't matter. Generally it's better to have more readable code than slightly faster code. Sep 29 '15 at 1:16

I had trouble with Daniel's answer in my Phone, but I have found the answer for the problem it

if (Input.GetMouseButtonDown (0)) {
if (UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject) return;
}


For me at least, the below solution worked just fine.. Elmar had it almost right. Hope it helps the next person to find this!

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class PlayerControl : MonoBehaviour {

void Update () {
if (Input.GetMouseButtonDown(0) && EventSystem.current.IsPointerOverGameObject() == false)
{
MovePlayer();
}
}
}


Slight difference for smartphone games: https://docs.unity3d.com/2018.2/Documentation/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html

While the other answers “Hack” a solution for that individual button, it doesn’t fix the overall problem of a click propagating after it’s been detected by a script that uses it.

The input system can “consume” events. So that they are used up when required.and don’t get passed on to other objects.

For example Event.Use method.

There is a good discussion of this on the following thread: