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))
{
Debug.Log(EventSystem.current.IsPointerOverGameObject());
agent.SetDestination(hit.point);
}
}
}
}
Anybody knows why?