Can't Give Click to Move to Nav Mesh Agent ?!?

I created this Click-to-move script for my NavMeshAgent but it doesn’t seem to work properly.

It will occasionally move to my click location. But, usually, my NavMeshAgent will do nothing. Sometimes I have to “click” 20 times over the course of many seconds and it just sits there. Sometimes it will move on the first click.

I’ve checked my mouse and it seems to be working properly. Can’t figure it.

Here’s the code if that helps:
{

public Vector3 GoToAllyPoint;
NavMeshAgent agent;

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

void Update ()
{

	if (Input.GetMouseButtonDown(0))
	{
		
		RaycastHit hit;

		if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
		{
			agent.SetDestination(hit.point);		
		}
	}
}

}

Okay, I re-baked it using different settings. Works fine now.

I have no idea why.

But it works exactly as I want it to now.

Hi @Topthink,
i was trying you code and everythings is fine.
Dont you forget to bake the navmesh maybe?

Try something :
open new project, add a plane, bake his navmesh
then create a capsule, add it a navmeshagent and your script, and so tell us if that work with this few component.