Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

NavMeshAgent placed on NavMesh but refuses to work

Discussion in 'Navigation' started by Tom163, Sep 3, 2020.

  1. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,256
    I'm getting the "SetDestination" can only be called on an active agent that has been placed on a NavMesh. error.

    I have tried various fixes suggested in this forum, including simply placing the darn thing right on the NavMesh and only then enabling the NavMeshAgent and the Behaviour Designer Tree:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class CreatureBehaviour : MonoBehaviour {
    7.  
    8.     public LayerMask layerMask;
    9.  
    10.     void Start() {
    11.         NavMeshHit hit;
    12.         if (NavMesh.SamplePosition(transform.position, out hit, 100f, NavMesh.AllAreas)) {
    13.             transform.position = hit.position;
    14.  
    15.             NavMeshAgent agent = gameObject.GetComponent<NavMeshAgent>();
    16.             agent.enabled = true;
    17.  
    18.             Behaviour bt = gameObject.GetComponent<Behaviour>();
    19.             bt.enabled = true;
    20.         } else {
    21.             // FIXME: serious error...
    22.         }
    23.     }
    24.  
    25.  
    26. }
    27.  
    I've also tried moving it in the scene view in all directions while game is running, just to check.

    No idea anymore what is going on. Anyone found a solution that definitely works?

    (and yes, the NavMesh is baked, that's the blue in the screenshot)

    Bildschirmfoto 2020-09-03 um 16.54.40.png
     
  2. thiagoCarelli

    thiagoCarelli

    Joined:
    Mar 11, 2023
    Posts:
    2
    Ingles -

    Hi, did you manage to fix the problem? I'm having exactly the same problem.
     
  3. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    255
    Wait for one frame until you switch on the behaviour. Unity needs the next frame to place the agent on the navmesh and activate it.