Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

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

Discussion in 'Navigation' started by lukagril, Nov 28, 2017.

  1. lukagril

    lukagril

    Joined:
    Nov 29, 2016
    Posts:
    46
    Hello,

    I am implementing navmesh navigation in my project. My map already has walkable area baked:



    I have a prefab with NavMesh Agent component and a script that sets it's destination:

    Code (CSharp):
    1. void Start(){
    2.     mBasePosition = GameObject.FindGameObjectWithTag ("Player").transform.position;
    3.     mNavMeshAgent = GetComponent<NavMeshAgent> ();
    4.     mNavMeshAgent.destination = mBasePosition;
    5. }
    When I put this prefab in a scene manually (drag into scene) and start the game , navigation works without problems. But when I instantiate it from a script at runtime (prefab spawner), I get a following error when trying to set destination:

    "SetDestination" can only be called on an active agent that has been placed on a NavMesh.
    UnityEngine.AI.NavMeshAgent:set_destination(Vector3)
    EnemyMovement:Start() (at Assets/Game/Scripts/Game/Enemy/EnemyMovement.cs:21)

    Any ideas how to solve this ?

    Regards, L
     
  2. Kyn-Creations

    Kyn-Creations

    Joined:
    Dec 19, 2012
    Posts:
    1
    I'm getting the same error currently, and have tried numerous methods of refreshing the nav agent component, nav mesh, and position of the game object. I'll post back if I solve it. My current hypothesis is that it has to do with OnEnable, since the game objects are being created from a pool.
     
  3. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    406
    My guess is that you're not exactly on the navmesh. Try using NavMesh.SamplePosition to fetch the closest location to mBasePosition that's actually on the mesh.
     
    GameDevCouple_I likes this.