IsPointerOverGameObject() returns always true.

Hello,

At some point of my game, the player has to click on any point of the screen to make the character move, so:

if (Input.GetMouseButtonDown(0) == true)
{
// do stuff.
}

Now, I've added the first UI element: a button. So: Canvas -> Panel -> Button. Clicking this button, makes the character move. So, the click input is going through the UI. Fine, I've found out I have to use IsPointerOverGameObject().

if (Input.GetMouseButtonDown(0) == true && !EventSystem.current.IsPointerOverGameObject())
{
// do stuff.
}

Now my character never moves, because IsPointerOverGameObject() always returns true.

May it be because even though I'm not clicking the button I'm still clicking the Canvas? What do I have to do then? I'm completely lost...

Thanks in advance.

1 Like

I'm just learning here too, but I found this:
https://answers.unity.com/questions/1429689/specific-ui-element.html

I would think you could just use the comparetag, but he says you need all three, or at least two. Well, I guess you would need to know if it was over an object.

1 Like

If the button makes the character move, I would suggest that you add an OnClick event for the button to go to your move method. You won't need Update, or MouseButtonDown, or the IsPointerOver for this part..
:)

2 Likes

[quote=“fire7side”, post:2, topic: 693565]
I’m just learning here too, but I found this:
https://answers.unity.com/questions/1429689/specific-ui-element.html

I would think you could just use the comparetag, but he says you need all three, or at least two. Well, I guess you would need to know if it was over an object.
[/quote]

This gave me an idea:

if (Input.GetMouseButtonDown(0) == true && EventSystem.current.IsPointerOverGameObject()) {
            Debug.Log (EventSystem.current.currentSelectedGameObject.gameObject.name);
        }

With this code, I should get the name of the game object I’m clicking. If I click the button I get the button’s name correctly. If I click anywhere else I get a NullReference like if I’m not clicking anything. I’m honestly confused.

[quote=“methos5k”, post:3, topic: 693565]
If the button makes the character move, I would suggest that you add an OnClick event for the button to go to your move method. You won’t need Update, or MouseButtonDown, or the IsPointerOver for this part…
:slight_smile:
[/quote]

No, that’s not it. I have a small button on the top left of the screen. This button, when clicked, should lock/unlock the camera. Clicking anywhere else on the scene should make the character move. The problem is: when I click on the button the camera is locked/unlocked AND the character moves. The character must not receive the input (and thus move) if I’m clicking an UI button.

1 Like

Ok, so you wan't the player to click anywhere on the screen, but you also want to have a menu button right?
And you don't want the charactor to move when you click the button?

Sounds like you need to have a "isMovable" check.

in your character's moving script, make a bool isMovable.
so check isMovable == True then move charactor.

So when you click your button toggle isMovable true/false.

seems its detecting you are clicking the button alright.

Based on the docs https://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html

You should simply check for the mouseClick and if it's not over a gameobject.

    private void Update()
    {
        if(Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
        {
            Debug.Log("Not over gameobject");
        }
        else if (Input.GetMouseButtonDown(0) && UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
        {
            Debug.Log("Over Gameobject");
        }
    }
}

So the above code works just fine. I added a UI button and clicked on it and it printed over gameobject. I then clicked outside the button and it printed I was not over a gameobject.

So the question is what are you using for your movement as that might be why you're having issues.

1 Like

[quote=“Brathnann”, post:6, topic: 693565]
Based on the docs https://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html

You should simply check for the mouseClick and if it’s not over a gameobject.

    private void Update()
    {
        if(Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
        {
            Debug.Log("Not over gameobject");
        }
        else if (Input.GetMouseButtonDown(0) && UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
        {
            Debug.Log("Over Gameobject");
        }
    }
}

So the above code works just fine. I added a UI button and clicked on it and it printed over gameobject. I then clicked outside the button and it printed I was not over a gameobject.

So the question is what are you using for your movement as that might be why you’re having issues.
[/quote]

This is exactly the problem. IsPoinerOverGameObject() is returning true even though it should not.

    if(Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
        {
            Debug.Log("Not over gameobject");
// move
        }

This ^ never happens. It is not a matter of how I am moving my character, because I have exactly the same code. I can’t even see that Debug.Log because IsPointerOverGameObject() is never false.

The only thing that “could” return true the Canvas that is above the scene, but at this point I would have noticed that by using IsPointerOverGameObject().gameObject.name .

EDIT: solved. In the hierarchy Canvas → Panel → Button, my Panel had the Image Script by default, apparently that was causing the problem. By disabling that script everything works, because I was clicking the image inside the Panel.

Thanks guys for your inputs. :slight_smile:

The reason you were detecting Input was because Raycast was enabled on the UI gameobject. Just disable Raycast for certain UI objects for eg bg image and it won't register a hit.

Check the Event Mask in the Physics Raycaster on your camera. IsPointerOverGameObject() will return true if the mouse is over any objects on those layers.

If you only want to check for the UI layer, make sure that only UI is checked.

6 Likes