Search Unity

“SetDestination” can only be called on an active agent that has been placed on a NavMesh.

Discussion in '2D' started by xVhn-Sky, Mar 13, 2021.

  1. xVhn-Sky

    xVhn-Sky

    Joined:
    Oct 8, 2017
    Posts:
    2
    hello, I have problems with a script the error is above in the title and these are my scripts

    Code (CSharp):
    1. public class spawnunits : MonoBehaviour
    2. {
    3.     public int crtUnits;
    4.     public GameObject units;
    5.     public woodunit wu;
    6.     public Vector3 vec;
    7.  
    8.     public void Start()
    9.     {
    10.         crtUnits = 0;
    11.         StartCoroutine(CoUpdate());
    12.     }
    13.  
    14.     IEnumerator CoUpdate()
    15.     {
    16.        
    17.         if (crtUnits < 20)
    18.         {
    19.             yield return new WaitForSeconds(1);
    20.             Instantiate(units, transform.position, Quaternion.identity);
    21.             yield return new WaitForSeconds(1);
    22.             Instantiate(units, transform.position, Quaternion.identity);
    23.             yield return new WaitForSeconds(1);
    24.             Instantiate(units, transform.position, Quaternion.identity);
    25.  
    26.         }
    27.         wu.GetWood();
    28.        
    29.         yield return null;
    30.     }
    31.    
    32. }
    Code (CSharp):
    1. public class woodunit : MonoBehaviour
    2. {
    3.     public GameObject treedest;
    4.     public GameObject homebase;
    5.     public NavMeshAgent unitagent;
    6.     public void Update()
    7.     {
    8.        
    9.     }
    10.     public void GetWood()
    11.     {
    12.         treedest = GameObject.Find("treeparent(Clone)");
    13.  
    14.         unitagent.SetDestination(treedest.transform.position);
    15.     }
    16.     public void ReturnHome()
    17.     {
    18.        
    19.     }
    20.    
    21. }
    22.  

    What they should do is that the first script should instantiate 3 Navmesh Units ( it does that good )
    then the second should set the units' location thru code towards a tree, my trees are randomly instantiated in the scene so I can't assign them in the inspector even If I wanted to, anyway after the 3 Navmesh Units spawn I get this

    “SetDestination” can only be called on an active agent that has been placed on a NavMesh.

    I've assigned everything correctly ( I hope ), I've used UnityEngine.AI, at first the script worked only when I told the units the location in a void Start() but when I do it in a custom function it does not work, also it does not work in void Update. any clue what can cause this? I work in 2D with a 2D NavMesh Plugin ( it is the same as a normal navmesh but rotated on the XY( like a wall) axis instead of XZ ( like a floor).
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,486
    Whether you're using 2D or 3D, the dedicated Navmesh forum is here.

    I can move your thread if you like.