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.
  2. Dismiss Notice

Resolved Navmesh becomes UNwalkable when I instantiate

Discussion in 'Navigation' started by Atix07, Aug 12, 2021.

  1. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    Hello!

    Ive been trying to fix this issue for a day now and Im stuck. When I instantiate ai prefab, area turns unwalkable for some reason.

    This is the error Im getting: "SetDestination" can only be called on an active agent that has been placed on a NavMesh.



    This is how I spawn:
    Code (CSharp):
    1.  public void spawnGhost()
    2.     {
    3.         NavMeshHit hit;
    4.         if (NavMesh.SamplePosition(spawnPoint.transform.position, out hit, 100f, NavMesh.AllAreas))
    5.         {
    6.              PhotonNetwork.InstantiateRoomObject(selectedGhostString, hit.position, Quaternion.identity);
    7.         }
    8.     }
    After that I even check if agent is on the navmesh:
    Code (CSharp):
    1. private void Start()
    2.     {
    3.         if (!ghostAgent.isOnNavMesh)
    4.             resetNavmeshAgent();
    5.  
    6.         ChangeState(idleState);
    7.     }
    8.  
    9.     private void resetNavmeshAgent()
    10.     {
    11.         Debug.Log("Reseting navmesh agent");
    12.         NavMeshHit hit;
    13.         NavMesh.SamplePosition(spawnPoint, out hit, 100f, NavMesh.AllAreas);
    14.         Vector3 warpPosition = hit.position;
    15.         ghostAgent.transform.position = warpPosition;
    16.         ghostAgent.enabled = false;
    17.         ghostAgent.enabled = true;
    18.     }
    Help please :(
    Thaks!
     
    Last edited: Aug 12, 2021
  2. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    Okay managed to fix it. Area becomes dark blue when agent is in that area. Somehow the referance I put in prefab does not count as referance and needed to get component at runtime and disable-reenable the agent worked that way. Weird but okay.