- Home /
I can't get the manual Nav Mesh example to work.
I keep getting a null reference exception. I've been working on this for several hours and I cannot get around the error.
Here's the link: https://docs.unity3d.com/Manual/nav-MoveToClickPoint.html
Here's what I've done. I created a 50x50 terrain and I "baked" the terrain to a pretty blue color. I created a primitive cylinder (I tried spheres, cubes, etc) and I added a nav mesh agent to the cylinder. I copied the text from the manual to my script (and I've tried many versions). The manual example shows a private NavMeshAgent but I've tried both private and public. I've dragged/dropped the cylinder into the inspector. I've dragged and dropped just about everything I can think of.
Here's the most recent code I've tried:
using UnityEngine;
using UnityEngine.AI;
public class NavMeshMove : MonoBehaviour
{
public NavMeshAgent myAgent;
void Start()
{
myAgent.GetComponent<NavMeshAgent>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1000))
{
myAgent.destination = hit.point;
}
}
}
}
Answer by idcanu · Nov 22, 2018 at 07:38 AM
using UnityEngine; using UnityEngine.AI;
public class NavMeshMove : MonoBehaviour { public NavMeshAgent myAgent;
void Start()
{
myAgent=GetComponent<NavMeshAgent>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1000))
{
myAgent.destination = hit.point;
}
}
}
}
Thanks, the problem was that for some reason the "$$anonymous$$ainCamera" tag wasn't applied to the main camera. I never had to manually add this before (that I can remember) so it was hitting the error...maybe it's just something new with the latest version or two of Unity. It took a long time for me to figure out the problem. You're the only person who even bothered to comment so I'm going to give you credit for the answer. Thanks for taking the time in and effort to assist me.