Search Unity

"Failed to create agent because it is not close enough to the Navmesh"

Discussion in 'Navigation' started by Kylotan, Oct 17, 2017.

  1. Kylotan

    Kylotan

    Joined:
    Feb 17, 2011
    Posts:
    212
    I have been getting this warning (followed by '"SetDestination" can only be called on an active agent that has been placed on a NavMesh' errors) since upgrading to 2017.2.0f3, on agents that worked fine in 2017.1.whatever.

    I read in some places that this can be fixed by rebaking the navmesh and being 100% sure the values for the step/slope/etc match the ones on the agent. I have done this, and it did not help.

    I am even using Navmesh.SamplePosition to get the exact position of the navmesh below my spawn point and passing that vector in to Instantiate, but it still sometimes gives me this error.

    What is going on? How can I debug this?

    (Any Unity, PLEASE can we improve this warning message? There's no exception so we can't catch it at runtime, it doesn't tell us which object it was so we can't just combine it with other logs, it doesn't tell us whether we are a centimetre or a kilometre away from the navmesh... it is just very unhelpful.)
     
    sarahnorthway likes this.
  2. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,976
    This warning message is already telling you a lot, and actually it is the most common of all navmesh errors.

    It means that your agent is not currently on the navmesh.

    It usually is because you are trying to use an agent that has been deactivated, or still calling an update to an agent that has since been removed from the scene or re positioned.

    You need to check all your code and make sure you are only setting destination etc when the navmesh agent is actually placed on the navmesh and its agent is enabled and ready to receive instructions.

    EDIT: try to make sure your not setting destinations or trying to use stuff inside of the agent before the object exists and all its components are started and enabled. If your instantiating then you need to control the flow better.
     
  3. Kylotan

    Kylotan

    Joined:
    Feb 17, 2011
    Posts:
    212
    No, this error is coming when I instantiate an object with a NavMeshAgent directly on the Navmesh, i.e. at the exact position where the Navmesh.SamplePosition call tells me it is.
     
  4. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,976
    I realise that you are instantiating on the mesh, as not doing so would obviously error out.

    No, I am saying that I often get this error when I have not been careful with instantiation.

    Create a unit test and test harness that does the instantiate at that location, and then does the same thing at y + 0.01 and y- 0.01 1000 or 2000 times over, and same for variations in X and Z position, with good debug logging for results (or better save to another log file for readability).

    Then you can tell if its actually because some other process (unity or otherwise) has actually moved the agent (or is misreporting for some reason).

    I always unit test navmesh agent placement on instantiation as its always finicky depending on what version of unity has been broken *cough* i mean updated *cough*
     
  5. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,976
    And also you have not said anything about everything else I said. When are you calling set destination and other navmesh agent specific calls?

    This must be done after everything is set up. Its often really easy to miss a call that fiddles with or relies on a navmesh agent, and unity will often not really tell you much when this happens other than it could not use the agent as it is not properly placed.

    EDIT:

    Also not properly stopping and starting the agent (using the wrong methods or trying at wrong point in flow) can cause this error to sometimes show (which seems like a unity error as its not really related)
     
  6. Kylotan

    Kylotan

    Joined:
    Feb 17, 2011
    Posts:
    212
    Okay, I obviously haven't spelled this out precisely enough, but this is happening inside the literal Instantiate call. The other calls to SetDestination etc are irrelevant because they haven't happened yet. I have a prefab with a NavMeshAgent, I create it on the NavMesh, Unity claims it's not close enough.
     
    anycolourulike and devotid like this.
  7. tim_p

    tim_p

    Joined:
    Sep 15, 2017
    Posts:
    1
    I've been searching for this problem today too.

    Just to make things very clear for others here is some sample code:

    Code (CSharp):
    1.         NavMeshHit hit;
    2.         NavMesh.SamplePosition(transform.position, out hit, 1.0f, 1);
    3.         var newObj = Object.Instantiate(agentPrefab, hit.position, Quaternion.identity);
    This generates the error on the last line (Instantiate).

    The most interesting thing for me is that if I do this:

    Code (CSharp):
    1. newObj.GetComponent<NavMeshAgent>().Warp(hit.position);
    Further calls to the NavMeshAgent (such as SetDestination) work correctly.

    This warning is very annoying however, and is cluttering our logs!
     
    Kylotan likes this.
  8. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,976
    Then I would file an issue report because that's obviously a bug and will probably get patched out quite soon, if there isnt already a patch / bug fix in the works.

    EDIT: could potentially be a compounded issue on top of this bug:

    https://issuetracker.unity3d.com/is...ion-in-awake-method-of-an-instantiated-prefab

    either way thats all I could find that could be related, so file an issue
     
  9. Multithreaded_Games

    Multithreaded_Games

    Joined:
    Jul 6, 2015
    Posts:
    122
    Long shot,but are you sure that the agent's 'type' and 'area mask' match up with the actual surface on which you're trying to place the object?
     
  10. Kylotan

    Kylotan

    Joined:
    Feb 17, 2011
    Posts:
    212
    Yes, these values are correct, and they are prefabs that worked perfectly fine with 2017.1.
     
  11. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,976
    File an issue and paste the issue into here so we can vote on it, or its likely nothing will get done about it any time soon.
     
  12. awake

    awake

    Joined:
    Dec 24, 2013
    Posts:
    49
    I had this issue just now. I had to place the prefab on the navmesh in the editor, re-apply the prefab, and then delete it. Seemed to work after that.
     
    bantus likes this.
  13. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,976
    Thats different. The OP was instantiating and setting up the agents at runtime, what your referring to is just simply not placing an agent on the navmesh, which as you can see by reading the posts, has already been mentioned multiple times, including by myself.
     
    awake likes this.
  14. Kylotan

    Kylotan

    Joined:
    Feb 17, 2011
    Posts:
    212
    I logged a bug, but apparently that isn't the same thing as an issue, so nobody can vote on it. It's not clear how we log issues, or why they are different things.
     
  15. Kylotan

    Kylotan

    Joined:
    Feb 17, 2011
    Posts:
    212
    Just to confirm Tim_P's workaround above, this pseudocode is what is currently working for me:

    Code (CSharp):
    1. var navMeshAgent = GetComponent<NavMeshAgent>();
    2. if (navMeshAgent.enabled && !navmeshAgent.isOnNavMesh)
    3. {
    4.     var position = transform.position;
    5.     NavMeshHit hit;
    6.     NavMesh.SamplePosition(position, out hit, 10.0f);
    7.     position = hit.position; // usually this barely changes, if at all
    8.     navMeshAgent.Warp(position);
    9. }
    It seems like the NavMeshAgent is somehow desynchronised from the gameObject's position, so it needs that Warp call to resync it.

    (EDIT: initially I typed position=position, obviously I meant position=hit.position)
     
    Last edited: Oct 25, 2017
  16. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,976
    Very weird, I have tried to replicate and from my own experiments it seems that agent.isOnNavMesh seems to be getting set to the reverse of what it should be, but I cant seem to see where this is happening.

    I will try looking through the unity decompiled github to get a better idea of why, but definately worth a look from the unity support team!

    https://github.com/MattRix/UnityDecompiled

    EDIT: It might not be an up to date branch so if not then get a copy of ILspy and you can easily decompile the code yourself

    EDIT 2: this post seems possibly related, https://forum.unity.com/threads/sampleposition-bug-or-undocumented-behavior.501705/

    worth an investigate!
     
  17. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    980
    Pretty sure this is a regression. It used to be an issue back in something like 5.3 and was later patched. I can almost recall reading the release notes of a later version about the fix and thinking 'Whateves'. Looks like its popped up again in 17.2
     
  18. hanbekov

    hanbekov

    Joined:
    Apr 1, 2017
    Posts:
    8
    I too get warning "Failed to create agent because it is not close enough to the Navmesh". A see - prefab spawn similar NavMesh. 0_0
    Then I use NavMesh.SamplePosition for get real NavMesh point. But no effect.
    And I find root of this error - unHumanoid agent type!
    I set agent type to Humanoid and script worked perfectly, without warning.
    But what will done? I need different unHumanoid agents type!
     
    drewvancamp, PPLorux and MrDizzle26 like this.
  19. cgascons

    cgascons

    Joined:
    Feb 22, 2016
    Posts:
    25
    Hi, I had the exact same issue and found out as well it was due to the fact of using a different Agent Type than humanoid. Switched back to that one and worked like a charm. Anyone know by the way why 2017.x version of Unity doesn't come with some NavMesh Components such as the NavMeshSurface? I believe Unity 5.x does have those included, but I'd like to keep using 2017.x.

    By the way @hanbekov there's the possibility of downloading the full package of NavMeshComponents directly from GitHub as explained here: https://issuetracker.unity3d.com/is...n-error-failed-to-create-agent-in-the-console

    Hope this helps
     
    MrDizzle26 likes this.
  20. Kylotan

    Kylotan

    Joined:
    Feb 17, 2011
    Posts:
    212
    ...bump...

    I don't suppose there's any chance of someone in Unity acknowledging this? I hear the Navmesh maintainer might have changed hands recently so maybe it's slipped through the cracks, or got introduced in the changeover.

    Since it seems to be fixable by Warping the agent, maybe it is indeed related to the position bug linked early in the thread, which may be a duplicate of this one - https://issuetracker.unity3d.com/is...ect-dot-instantiate-after-awake-function-call - and that seems to have been fixed in a patch release, but that patch isn't availabler for 2017.2 yet.
     
    MadeFromPolygons likes this.
  21. BenSizerRovio

    BenSizerRovio

    Joined:
    Sep 25, 2017
    Posts:
    12
    Fixed in the latest patch release. Looks like the position bug, now fixed, was the culprit.
     
    MadeFromPolygons likes this.
  22. richardzzzarnold

    richardzzzarnold

    Joined:
    Aug 2, 2012
    Posts:
    140
    I still have this problem in Unity 2019.1.0a13
     
    Regone and skydestinies like this.
  23. skydestinies

    skydestinies

    Joined:
    Feb 6, 2014
    Posts:
    19
    I'm also getting this when the navmesh agent is created. Since unity doesn't give us any creation call before "Awake," I have switched to using Start() as when the object's information should be considered finalized. But the NavMesh Agent's warnings seem to break this convention, doing a check for whether the agent is in a valid location on Instantiate instead of Awake() or Start(). If Unity really doesn't want us to have a call to set up an object fully before Awake, then i don't see a way around this warning.
     
    Last edited: May 12, 2019
  24. Kylotan

    Kylotan

    Joined:
    Feb 17, 2011
    Posts:
    212
    Instantiate does allow you to pick a position, so it's reasonable for Unity to expect you to have positioned an Instantiated NavMeshAgent correctly. The problem I was having was a specific Unity regression where Instantiate was setting the new object's position after creating the NavMeshAgent (or something like that), meaning that even if you Instantiated directly onto a NavMesh, it would say there was no NavMesh there. This has been fixed since - unless you can repro it in 2019?
     
  25. Regone

    Regone

    Joined:
    Aug 1, 2014
    Posts:
    35
    Same in Unity 2019.1.4f1
    I make a new type of agent and nothing is working.
    when setting all agents back to humanoid everything is working.
     
    MrDizzle26 likes this.
  26. sarahnorthway

    sarahnorthway

    Joined:
    Jul 16, 2015
    Posts:
    78
    I'm getting this in 2021.1.27f1, after loading a new additive scene beneath my agent I do:

    Code (CSharp):
    1. bool success = NavMesh.SamplePosition(randomPoint, out NavMeshHit navHit, 10, NavMesh.AllAreas);
    2. if (success) {
    3.     agent.Warp(navHit.position);
    4.     Debug.Log("Is the Agent on the NavMesh? " + agent.isOnNavMesh); // no it is not
    5. }
    Intermittently, if randomPoint is near the edge of the navmesh, SamplePosition will return a navHit with the wrong Y value, placing the agent slightly below the navmesh. The call to agent.Warp moves the transform, the agent appears stuck halfway below the ground, and I get the warning "Failed to create agent because it is not close enough to the NavMesh".

    If I call NavMesh.SamplePosition(transform.position) immediately, it still returns the same below-ground navHit.position. If I wait until the next Update then call it again, it returns the correct position above the ground.

    Could be related to this NavMeshObstacles bug, since I have one in this scene. All I know is it has nothing to do with the agent itself, it's the call to SamplePosition which is temporarily returning the wrong result. The bug is intermittent and I can't recreate it by replacing randomPoint with a hardcoded position where it failed before. Identical agents spawned beside it during the same loop are usually fine.

    For anyone else encountering this error - try waiting a frame then sampling the same position again.
     
  27. MrDizzle26

    MrDizzle26

    Joined:
    Feb 8, 2015
    Posts:
    36

    I'm having this issue in 2021.3.7f1!

    Did you find a way to use non-humanoid NavAgents?
     
  28. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,316
    Same here in 2020.3.37. I bought AStar.
     
  29. MrDizzle26

    MrDizzle26

    Joined:
    Feb 8, 2015
    Posts:
    36
    I found out that to use multiple agent types you have to use the experimental NavMesh package. I know there is a hint for this in the bottom tray when in the navigation window, but it is not clear that it is necessary for using the multiple agent types feature.

    Why allow users to create multiple agent types out of the box if the feature isn't supported without an additional package, it doesn't make sense, least not to the people in this thread.

    The nav system could sure use modernizing, those planned reworks in the roadmap can't come soon enough!
     
    FardinHaque likes this.
  30. nalex66

    nalex66

    Joined:
    Aug 26, 2022
    Posts:
    54
    Thank you for flagging that! I just added a new agent type and have been getting swamped with this error message. I'll be adding the experimental nav package later in my project, so I guess for now I'll just make all my agents "Humanoid" so I can get on with it.