NavMeshAgent — Custom Agent Type SetDestination() Error in Unity

Chris Hilton
3 min readSep 13, 2023

--

Objective: To help you resolve this frustrating error when using a custom Agent type in Unity.

I must admit that this problem had me stumped for a while and it wasn’t until I created a blank project and started comparing the differences between the NavMeshAgent components in an effort to debug the issue and it paid off! I spent hours scrolling through forum solutions only to fall short with all of their recommendations.

Let’s set the scene so you know what we are working with.

Problem

I was wanting to use custom agent types for my NavMesh system so that I could build individual walkable paths for the enemies reaching the end goal. So instead of using the normal ‘Humanoid’ I created my own:

This is where this error creeped in and stayed for a while:

The error message received gave no indication that is was a custom agent type issue and instead pointed to the fact that:

  • I may have not baked the NavMesh.
  • My agent is not active on the NavMesh. Let’s enable and disable the GameObject and see what happens.
  • Is the model too low or too high on the NavMesh so that it isn’t finding it on the surface? Let’s move it incrementally below and above the NavMeshSurface and see if it picks it up.
  • Is it a model issue? Let’s also test to see if primitives work.
  • Could it be a collider issue? Let’s test the different colliders.
  • Could it be because we are calling GetComponent<NavMeshAgent>() and SetDestination() both in the Start() method. Let’s move the GetComponent call to the Awake() method and test.
  • Maybe NavMeshAgent.Warp() will help reset the models position on the NavMeshSurface and it will pick it up.

Nothing worked! I went through all these solutions and fell short until I found this buried deep from 6 years ago…

Solution

Custom NavMeshSurface component and linking it to my custom agent type.

You can create this by right clicking in the Hierarchy window → AI → NavMeshSurface

This will create a separate GameObject that sits in the Hierarchy window with the NavMeshSurface component attached to it:

Here I was able to set the ‘Agent Type’ to the custom agent type I created in the Navigation panel window in Unity:

I was then able to drag the GameObjects I wanted as a child to the NavMeshSurface parent in the Hierarchy:

Then setting the ‘Collect Objectsfield to ‘Children’. This will bake the children GameObjects for this particular NavMeshSurface.

Don’t forget to hit ‘Bake’ down the bottom.

Hope this helps someone!

NOTE: If anyone from Unity reads this, please update the error message to be more specific.

Links

NavMeshSurface Component

--

--