SetDestination can only be called on an active agent that is on a NavMesh

Hello, im trying to make that the enemy is following the player with a script and a Nav mesh agent but its not working i got this error:
SetDestination" can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.AI.NavMeshAgent:SetDestination(Vector3)
EnnemyController:Update() (at Assets/Script/EnnemyController.cs:23)

My script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class EnnemyController : MonoBehaviour {

    public float lookRadius = 10f;

    Transform target;
    NavMeshAgent agent;

    void Start () {
        target = PlayerManager.instance.player.transform;
        agent = GetComponent<NavMeshAgent>();
    }
  
    void Update () {
        float distance = Vector3.Distance(target.position, transform.position);

        if (distance <= lookRadius)
        {
            agent.SetDestination(target.position);
        }
    }

    void OnDrawGizmosSelected ()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, lookRadius);

    }
}
1 Like

Try moving the agent up or down a bit in the scene. Sometimes it doesnā€™t register as on the nav mesh if the position isnā€™t quite right.

2 Likes

Im gonna try

Not working

I found a solution, Window, AI, Navigation, bake, select the floor and then click on bake

30 Likes

You told us you already have a NavMeshā€¦ :smile:

3 Likes

Ah yes, not having a nav mesh at all will cause the same issue. I should have mentioned that.

3 Likes

Not baking means there is no navmesh. So your problem was actually that you were calling set destination with no navmesh present in the scene :slight_smile:

Iā€™m still new to Unity, what else can I do to solve these kinds of problem? I tried doing the Window, AI,ā€¦ solution but it did nothing. I also tried moving the NavMeshAgent up/down in the inspector menu but it shows a window that says ā€˜Cannot restructure prefab instanceā€™ : ā€œChildren of Prefab instance cannot beā€¦ā€ When I clicked Open Prefab, The inspector only showed the Transform and Animator. It did not show the NavMeshAgent nor the Script. Please help me

You must edit the prefab in order to make those changes, Click on the prefab and edit it.

For me, it solved by this:
Maybe the person is a prefab and you are instantiating at runtime. This way, Reset the Transform of the prefab in such a way that he is touching the ground. Whether or not a person is a prefab, His X,Y,Z of transform should be in such a way that his feet touching the ground.

5173121--513383--4654654.png
5173121--513386--Annotation 2019-11-14 095322.png

1 Like

Another solution for this problem,

Your objectā€™s capsule colliderā€™s radius / height and the navmesh agentā€™s colliderā€™s radius / height has to match.

For example my enemyā€™ s capsule colliderā€™s radius was 39 and height was 223. Nav Mesh Agentā€™s radius was 0,5 and height was 2,0 so always got an error such as SetDestination" can only be called on an active agent that has been placed on a NavMesh. After i changed nav mesh agentā€™s radius and height to my enemy objectā€™ s collider values, everything worked perfectly.

9 Likes

This will also happen if the agent is placed on a navmesh that has a different agent type than the agent itself

9 Likes

Thanks for the fix. I started with a modular scene and the instantiate to spawn points worked fine.(2018.4.13f1) Once I changed to a terrain, the spawn still worked but the enemies could not see the player and did not move. The fix was to add the prefabs back to the new terrain and create new prefabs (deleted the original prefabs). Of course I had to attach the new prefabs to the object references in the inspector since using [serialized fields].

That was our problem.
Any specific steps to take to fix this?
I havenā€™t been able to figure out how to create separate NavMesh in one scene? That was our original goal. It used to be done using the ā€œNavMeshSurfaceā€ component on an Empty GameObject. You could have two or three of these in your scene.

Now though, it is a Navigation panel that seems to be one single NavMesh for the entire scene. Yet that means only one type of enemy can use the NavMesh and the others say there is no NavMesh at all.

problem fixed here :

3 Likes

I have done everything listed above but it still does not work, I have the same script as Tornado77, I have a NavMesh set on the floor of my scene, my character is touching the ground, my capsule colliderā€™s radius is the same as the height of my navmesh agentā€™s colliderā€™s radius. What can Id do to fix this? (I also have the error: ā€œFailed to create agent because it is not close enough to the NavMeshā€)

It worked!

You watched the brackeys video didnā€™t you. I did as well and am having the same problem. Maybe that was only for an old version of unity.

to anyone coming here in the future make the platform static and then bake the navmesh, you should be able to see bluish path in the scene.

5 Likes