IsPointerOverGameObject() returns Object reference not set to an instance of an object

Hi guys,

I have a GUI button and every time, when I try to click on this button, my player moves.
So I’m trying to use IsPointerOverGameObject() to “block” the click movement.

I don’t know why, but when I click, I got this error in line 23, the Debug.Log line.

NullReferenceException: Object reference not set to an instance of an object

Here is my code:

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

public class PlayerMovement : MonoBehaviour
    NavMeshAgent agent;

    void Start()
        agent = GetComponent<NavMeshAgent>();

    void Update()
        if (Input.GetMouseButtonDown(0))
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, 100))

Anybody knows why?


I add the component to my player object and now looks like its working. No error messages.


I’m not getting errors but, I’m always receiving FALSE, even if I click inside the close button.


I would sincerely like to thank you for a no BS answer on this my man.
Adding an event system to the calling scripts object did it just fine. I am though, a little perturbed that this is documented no where, and in both the hex tutorials I am watching the instructor was not forced to do this.