"SetDestination can only be called on an active agent that has been placed on a NavMesh" ERROR

Hello,

I am implementing navmesh navigation in my project. My map already has walkable area baked:

I have a prefab with NavMesh Agent component and a script that sets it’s destination:

void Start(){
    mBasePosition = GameObject.FindGameObjectWithTag ("Player").transform.position;
    mNavMeshAgent = GetComponent<NavMeshAgent> ();
    mNavMeshAgent.destination = mBasePosition;
}

When I put this prefab in a scene manually (drag into scene) and start the game , navigation works without problems. But when I instantiate it from a script at runtime (prefab spawner), I get a following error when trying to set destination:

“SetDestination” can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.AI.NavMeshAgent:set_destination(Vector3)
EnemyMovement:Start() (at Assets/Game/Scripts/Game/Enemy/EnemyMovement.cs:21)

Any ideas how to solve this ?

Regards, L

I’m getting the same error currently, and have tried numerous methods of refreshing the nav agent component, nav mesh, and position of the game object. I’ll post back if I solve it. My current hypothesis is that it has to do with OnEnable, since the game objects are being created from a pool.

My guess is that you’re not exactly on the navmesh. Try using NavMesh.SamplePosition to fetch the closest location to mBasePosition that’s actually on the mesh.

1 Like

Anyone find a solution?

I did what Viscarious said in this thread:

Uncheck Nav Mesh Agent in the prefab then in the script, in the Start method, enable it.

3 Likes