Search Unity

IsPointerOverGameObject() returns always true.

Discussion in 'Scripting' started by hedgeh0g, Mar 2, 2018.

Thread Status:
Not open for further replies.
  1. hedgeh0g

    hedgeh0g

    Joined:
    Jul 18, 2014
    Posts:
    102
    Hello,

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

    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0) == true)
    2. {
    3. // do stuff.
    4. }
    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().

    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0) == true && !EventSystem.current.IsPointerOverGameObject())
    2. {
    3. // do stuff.
    4. }
    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.
     
    scrant likes this.
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Indigocoder1 likes this.
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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..
    :)
     
    Benawtopia and firejerm like this.
  4. hedgeh0g

    hedgeh0g

    Joined:
    Jul 18, 2014
    Posts:
    102
    This gave me an idea:

    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0) == true && EventSystem.current.IsPointerOverGameObject()) {
    2.             Debug.Log (EventSystem.current.currentSelectedGameObject.gameObject.name);
    3.         }
    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.

    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.
     
    Benawtopia likes this.
  5. firejerm

    firejerm

    Joined:
    Dec 28, 2012
    Posts:
    43
    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.
     
    Last edited: Mar 3, 2018
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    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.
    Code (CSharp):
    1.  
    2.     private void Update()
    3.     {
    4.         if(Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
    5.         {
    6.             Debug.Log("Not over gameobject");
    7.         }
    8.         else if (Input.GetMouseButtonDown(0) && UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
    9.         {
    10.             Debug.Log("Over Gameobject");
    11.         }
    12.     }
    13. }
    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.
     
    firejerm likes this.
  7. hedgeh0g

    hedgeh0g

    Joined:
    Jul 18, 2014
    Posts:
    102

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

    Code (CSharp):
    1.     if(Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
    2.         {
    3.             Debug.Log("Not over gameobject");
    4. // move
    5.         }
    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. :)
     
    Last edited: Mar 3, 2018
  8. umarfarooq412

    umarfarooq412

    Joined:
    Nov 19, 2016
    Posts:
    5
    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.
     
  9. Prog_Meisters

    Prog_Meisters

    Joined:
    Mar 25, 2013
    Posts:
    8
    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.
     
Thread Status:
Not open for further replies.