IsPointerOverGameObject Problem

Hi, I have a click to move top down mobile project I’m working on, with some problems. You see I have a UI Background Canvas that holds my game background (UI image) to keep it in place (aspect ratio) for any mobile devices, I also have a UI pause button. I use Input.GetMouseButtonDown (0) to move my player around the game, however when I click on my pause button my player will move towards it striaght after I click resume. I thought of using IsPointerOverGameObject but It stops my player from moving around my game because my game background is an UI image. So is there a way of pausing my game without the player moving towards the pause button when I resume the game. Maybe like having IsPointerOverGameObject over a certain UI. Thank you, :slight_smile:
This is my PlayerMovement script:

    private Animator anim;
    public static float speed = 5f;
    public static Vector3 target;
    public PlayerMovement playerMovementRef;
    //public GameObject firstSelectedGameObject;
    private bool touched;
   
    void Start () {
        target = transform.position;
        anim = GetComponent<Animator> ();
    }
   
   
    public void Update () {
            if (Input.GetMouseButtonDown (0)) {
            if (!EventSystem.current.IsPointerOverGameObject ()) {
                        Vector3 mousePosition = Input.mousePosition;
                        mousePosition.z = 10; // distance from the camera
                        target = Camera.main.ScreenToWorldPoint (mousePosition);
                        target.z = transform.position.z;
            }
        }

            transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.fixedDeltaTime);
       
}

Since your pause button is a button, that means its a selectable Item. As soon as you click it the EventSystem sets it to selected I think this should work. (as long as the EventSystem is setting the selected object before the Update is called – which I don’t know enough about Unity internals to tell you. Can just try it and see)

if (EventSystem.current.currentSelectedGameObject == pauseButton.GameObject)
    return;
1 Like

Thank you for the reply, but I’m getting an error:
error CS1061: Type UnityEngine.UI.Button' does not contain a definition for GameObject’ and no extension method GameObject' of type UnityEngine.UI.Button’ could be found (are you missing a using directive or an assembly.

Once again Thank you for your help. :slight_smile:

No, Wait…It Worked!!! Thank you :slight_smile: :slight_smile: :). Your a HUGE help. Thank you

Um sorry about this but what if I want to add more button and so my player will move towards those buttons. I want to add two more button (power up buttons). Thank you again :slight_smile:

No wait nevermind I fixed it Thank you once again. :slight_smile:

hehe. yeah i did have a bug… it should have been lowercase gameObject ( pauseButton.gameObject) but i think you figured it out. One easy way is to have a List buttonList that is populated with all your button game objects… the loop through that list and check EventSystem.selected and return if its any of them.

1 Like