Search Unity

  1. Full schedule for #UniteBerlin is now available! Featuring talks on our roadmap, hands-on labs and much more! Check it out!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. 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 lukson666, Nov 28, 2017.

  1. lukson666

    lukson666

    Joined:
    Nov 29, 2016
    Posts:
    45
    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:
    126
    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.
     
    Daemonhahn likes this.