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

ConsoleRuntime exception · Navigation

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

The NavMeshAgent is not standing on a NavMesh it can use, or it is disabled, so it cannot plan a path. The agent snaps to the NavMesh when it is enabled; if there is no NavMesh close enough, it stays off it.

Read the Console line first

Select the agent in Play Mode. If it is not on the blue NavMesh overlay in the Scene view, that is the problem.

Common causes

  1. There is no baked NavMesh

    Add a NavMeshSurface component (from the AI Navigation package) and click Bake. Without a baked surface, no agent can path.

  2. The agent spawned away from the mesh

    Instantiating or moving an agent by setting transform.position can leave it off the mesh. Use NavMeshAgent.Warp, and NavMesh.SamplePosition to find the nearest valid point.

  3. The agent is disabled or its GameObject is inactive

    Calls on a disabled agent fail. Enable it first, or skip the call while it is disabled.

  4. The agent type does not match the baked surface

    A NavMeshSurface bakes for one agent type. An agent of another type cannot use it.

The fix

Fix
C#
if (NavMesh.SamplePosition(spawnPoint, out var hit, 2f, NavMesh.AllAreas))
    agent.Warp(hit.position);

if (agent.isActiveAndEnabled && agent.isOnNavMesh)
    agent.SetDestination(target.position);

Stop it coming back

Check agent.isOnNavMesh before path calls, and place spawned agents with Warp.

Questions

The agent was on the NavMesh, then I moved it and the error started. Why?
Setting transform.position directly can take the agent off the mesh. Move agents with agent.Warp(position), which places the agent on the NavMesh at that point.
How do I check before calling SetDestination?
Test agent.isActiveAndEnabled && agent.isOnNavMesh first. If it is false, wait a frame after spawning, or snap the spawn point with NavMesh.SamplePosition.
Where did the Navigation bake window go?
Since Unity 2022.2, NavMesh baking belongs to the AI Navigation package: add a NavMeshSurface component to your level and press Bake. The old bake tab survives only as a deprecated window, so new projects should use NavMeshSurface.

Scripts that avoid it

Related errors

Got a different error? Paste it into the error lookup.