Search Unity

"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:
    48
    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:
    966
    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.
     
    MadeFromPolygons likes this.
  4. ricky107

    ricky107

    Joined:
    Jul 21, 2016
    Posts:
    1
    Anyone find a solution?
     
  5. gOscar

    gOscar

    Joined:
    Jun 17, 2018
    Posts:
    2
    smmtest, pixelR and FlightOfOne like this.