Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Navigation' started by Tornado77, Dec 17, 2018.

  1. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    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:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class EnnemyController : MonoBehaviour {
    7.  
    8.     public float lookRadius = 10f;
    9.  
    10.     Transform target;
    11.     NavMeshAgent agent;
    12.  
    13.     void Start () {
    14.         target = PlayerManager.instance.player.transform;
    15.         agent = GetComponent<NavMeshAgent>();
    16.     }
    17.  
    18.     void Update () {
    19.         float distance = Vector3.Distance(target.position, transform.position);
    20.  
    21.         if (distance <= lookRadius)
    22.         {
    23.             agent.SetDestination(target.position);
    24.         }
    25.     }
    26.  
    27.     void OnDrawGizmosSelected ()
    28.     {
    29.         Gizmos.color = Color.red;
    30.         Gizmos.DrawWireSphere(transform.position, lookRadius);
    31.  
    32.     }
    33. }
    34.  
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    oscsr and Shado_Uchiha like this.
  3. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    Im gonna try
     
  4. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    Not working
     
  5. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    I found a solution, Window, AI, Navigation, bake, select the floor and then click on bake
     
  6. You told us you already have a NavMesh... :D
     
    arkano22, Bunny83 and kiiriito like this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Ah yes, not having a nav mesh at all will cause the same issue. I should have mentioned that.
     
  8. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,945
    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 :)
     
  9. fasmilano

    fasmilano

    Joined:
    Sep 7, 2019
    Posts:
    3
    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
     
  10. cmyd

    cmyd

    Joined:
    Oct 29, 2017
    Posts:
    98
    You must edit the prefab in order to make those changes, Click on the prefab and edit it.
     
  11. USMANHEART

    USMANHEART

    Joined:
    Jul 23, 2016
    Posts:
    56
    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**.
     

    Attached Files:

    kikchitov likes this.
  12. suleymanbucuk

    suleymanbucuk

    Joined:
    Mar 13, 2019
    Posts:
    11
    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.
     
  13. altschuler

    altschuler

    Joined:
    Nov 17, 2017
    Posts:
    7
    This will also happen if the agent is placed on a navmesh that has a different agent type than the agent itself
     
  14. bantus

    bantus

    Joined:
    Aug 16, 2017
    Posts:
    7
    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].
     
  15. bdilloughery_mvla

    bdilloughery_mvla

    Joined:
    Sep 22, 2017
    Posts:
    38
    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.
     
  16. ahmedaniss

    ahmedaniss

    Joined:
    Sep 18, 2019
    Posts:
    98
    problem fixed here :
     
    eliteforcevn, Sid1719 and MarkSharpe like this.
  17. LDFURI

    LDFURI

    Joined:
    Jun 22, 2020
    Posts:
    1
    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")
     
  18. Shado_Uchiha

    Shado_Uchiha

    Joined:
    Dec 22, 2020
    Posts:
    1
    It worked!
     
  19. robotscott725

    robotscott725

    Joined:
    Nov 4, 2020
    Posts:
    23
    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.
     
  20. Aashishsinng

    Aashishsinng

    Joined:
    Aug 28, 2018
    Posts:
    5
    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.
     
  21. SanjulaJeter

    SanjulaJeter

    Joined:
    Jul 24, 2020
    Posts:
    2
    it worked when i click on bake.
    thanks
     

    Attached Files:

    zheertahir1 likes this.
  22. cassius

    cassius

    Joined:
    Aug 5, 2012
    Posts:
    125
    None of these have worked for me. I've baked the navmesh multiple times and it's always clearly visible. My character is definitely intersecting with the navmesh. I've gone through all my Agent Types. Radius is at 0.5. Strange thing is that it was working 24 hours ago for me.
     
  23. Dasith98

    Dasith98

    Joined:
    Mar 13, 2021
    Posts:
    1
    Worked...
     
  24. darckangel211

    darckangel211

    Joined:
    Aug 7, 2020
    Posts:
    4
    i checked static and worked for me
     
  25. m0m01889

    m0m01889

    Joined:
    May 7, 2020
    Posts:
    2
    For those who watched Brackeys tutorial and still struggle: The first comment under the video rescued me!

    it says the following:

    For anyone that doesn't have "NavMeshSurface": A) don't download the script from github, that's just nasty. B) Assuming you have all the right components (NavMeshAgent, Walls and something to walk on). Next, in the toolbar, click on Window > AI > Navigation. this will open up a new tool. Click on Object (in the new tool window) and select the surface in the scene. Check Navigation static and make sure "Walkable" is selected for the 'Navigation Area' drop-down. Now click on the walls in your scene, check static and change to Not Walkable. Now go to the "Bake" tab, on the left of 'Object' and click bake. If your Agent is clipping through the walls, you can always increase "Agent Radius" in the Bake Tab.
     
    Uisce and vayaniumar like this.
  26. zheertahir1

    zheertahir1

    Joined:
    May 22, 2021
    Posts:
    1
    yesss it worked !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     
  27. vayaniumar

    vayaniumar

    Joined:
    Apr 25, 2021
    Posts:
    1
    Yup it works
     
  28. Seeliefy

    Seeliefy

    Joined:
    Dec 2, 2012
    Posts:
    3
    About this problem, this is not exactly the topic, but I think it is worth mentioning, I had the same error message, but I was instantiating an enemy with the NavMeshAgent already set, and it caused this error, I just had to enable the component by the inspector that already corrected, but I needed to solve it without doing this way, so the viable solution was to check if you have the NavMeshAgent on the enemy, destroy it, Create another identity at runtime, but Disabled, make an Invoke of 1s more or less to enable the NavMeshAgent again and setting the DESTINATION of the component, this solves the problem and without errors.

    I hope I helped someone.
     
    KaryaNeonPlay likes this.
  29. adventurefan

    adventurefan

    Joined:
    Jan 17, 2014
    Posts:
    229
    Another thing that can mess it up, is you're testing something with dynamic nav meshes instead of the baked ones... make sure you're always accounting for the fact that generating that mesh takes a tiny bit of time at start up so don't be running any agents commands before it's ready.
     
  30. br222

    br222

    Joined:
    Mar 26, 2020
    Posts:
    20
    I have this error as well. After hours of struggling I finally found out what happened.
    It's my enemy's state machine. When my enemy is in patrol state and moving around, and he meets my player and change into chase state, everything's fine. But when the enemy is taking a short break between one patrol and another (and he's agent.enabled = false) and just at that time my player passing by, he changes into the chase state and this error occurs.
    After finding out what happened, it's quite easy to fix. I added agent.enabled = true in the first line of the enemy's chase state, and everything is fine now.
    The tricky part is when I saw this error, I thought it's a navmesh agent setting error and never thought it might be a state machine bug until I happened to notice that the error only occurs when the player is in the enemy's lookRadius and he's not moving. So I write it down hoping can help people who meet a similar situation.
     
    Ayz8 and its3zin_ like this.
  31. aceghost360

    aceghost360

    Joined:
    Feb 24, 2021
    Posts:
    3
    Okay guys... Figured it Out!!!!
    I used a script to instantiate the navMesh agent and thet is how i got the error

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

    The whole problem was not in the navMeshAgent Component but was actually in the Instantiate component.
    The reason the error occurred was since the Instantiate method i used was

    "public static Object Instantiate(Object original, Transform parent);"

    Since I just entered a transform component as a parameter it didn't do anything to the spawn position.

    So use this Instantiate Method when spawning a navMeshAgent:

    "public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);"

    Hope You Found This Helpful!!
     
    mingyo0125, Burgezz and AlxQ14 like this.
  32. gojoe6404

    gojoe6404

    Joined:
    Jan 13, 2022
    Posts:
    2
    THANK YOU!!!!!!!!
     
  33. Monsterwald

    Monsterwald

    Joined:
    Jan 19, 2020
    Posts:
    68
    I have the most simple code and it does not work, no matter what I try... why does life hate me so much? Everything is setup, I have a blue navmesh, everything static, a navmeshagent, touching the navmesh.....

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class EnemyManager : MonoBehaviour
    7. {
    8.     [SerializeField] NavMeshAgent agent;
    9.     [SerializeField] GameObject target;
    10.  
    11.     private void Update()
    12.     {
    13.         agent.SetDestination(target.transform.position);
    14.     }
    15. }
     
    USMANHEART likes this.
  34. sunwooz

    sunwooz

    Joined:
    Nov 9, 2012
    Posts:
    10
    My problem was that I was instantiating a prefab and then referencing the prefab instead of the instantiated object.

    You can save the reference to the cloned instantiated object like so:

    subjectObject = Instantiate(currentSubject, new Vector3(-0.18f, 0f, -1.01f), Quaternion.identity);

    and use it later.
     
  35. cogito09

    cogito09

    Joined:
    Aug 30, 2017
    Posts:
    4
    Athomield3D and Krugmeisteren like this.
  36. Hansedison02

    Hansedison02

    Joined:
    Mar 28, 2022
    Posts:
    2
  37. metalanca

    metalanca

    Joined:
    Jun 9, 2020
    Posts:
    8
    Just to reiterate to any future people needing help because I missed this and wasted an extra 15 minutes:

    The "Bake" fix works, but you need to make sure your ground mesh has the "Static" box ticked ON before baking it. If you try to bake the ground and no blue overlay displays, it's probably because the "Static" box is not ticked. Tick that on, bake the mesh, and your error should be solved.
     
  38. nathan2019spence

    nathan2019spence

    Joined:
    Jan 21, 2022
    Posts:
    19
    Hi do you know how to do it if it is for a generic agent instead of humanoid?
     
  39. HsynAlty

    HsynAlty

    Joined:
    Dec 13, 2021
    Posts:
    1
    if u use a humanoid navmesh agent, this means that your agent's shape is like a cylinder. then u should use capsule collider instead of box collider etc. In addition to that, you should be careful about the size of your collider and agent.
     
  40. odaimoko

    odaimoko

    Joined:
    Jun 28, 2020
    Posts:
    8
    For me, it's select the corresponding AgentType for NavmeshAgent and NavMeshSurface.
    eg. Dont select AgentType Humanoid on NavmeshAgent, and select some other AgentType of NavMeshSurface.
     
  41. unity_CD7CA852FABD8AADF1FB

    unity_CD7CA852FABD8AADF1FB

    Joined:
    Oct 21, 2022
    Posts:
    1
    YOU ARE ACTUALLY THE MESSIAH! I'VE BEEN SPENDING SO LONG TRYING TO DO THIS AND YOU FIXED IT
     
  42. unity_v1SbKQhi831A8A

    unity_v1SbKQhi831A8A

    Joined:
    Aug 4, 2022
    Posts:
    1
    What the PlayerManager do???
     
  43. adaimmohassine

    adaimmohassine

    Joined:
    Sep 25, 2022
    Posts:
    1
    i have this problem
     

    Attached Files:

  44. Ayz8

    Ayz8

    Joined:
    Sep 23, 2022
    Posts:
    1
    Thank you my friend
     
  45. oscsr

    oscsr

    Joined:
    Jan 9, 2023
    Posts:
    1
    although this wasnt the issue that OP was having, it helped me so thank you.
     
  46. jongning

    jongning

    Joined:
    Jun 14, 2022
    Posts:
    7
    Thx That help me alot
     
  47. Krugmeisteren

    Krugmeisteren

    Joined:
    Mar 23, 2013
    Posts:
    2
    I had a similar problem and also solved it by updating the NavMesh agent type area masks.
     
  48. redders2312

    redders2312

    Joined:
    Jun 5, 2022
    Posts:
    2
    Hi, i've had similar problems after doing the zombie FPS course from gamedevtv on Udemy. Similar issue I think to br222. When my last shot that killed the zombie happens, the game broke with the same error. The navMeshAgent.enabled is set to = false which is good to stop the zombie continuing to move but there were other calls continuing in EngageTarget() then ChaseTarget() and that kind of thing. Was actually a really easy fix and all i had to do was add a line to say isProvoked = false as then EngageTarget() stops being called.
     
  49. Esteban-Gallardo

    Esteban-Gallardo

    Joined:
    Feb 19, 2013
    Posts:
    47
    This one worked for me in Unity 2022.3.2f1 LTS. Thank you!!
     
  50. michelle_sunshine

    michelle_sunshine

    Joined:
    Aug 31, 2023
    Posts:
    1
    solved