Search Unity

NavMesh stops working after rebuilding too often

Discussion in 'Navigation' started by dragon_slumber, Nov 23, 2018.

  1. dragon_slumber

    dragon_slumber

    Joined:
    Jun 11, 2017
    Posts:
    30
    Hey everyone,

    I'm working on a game with procedurally generated levels and using NavMesh for my enemies. I've noticed that on the first few iterations, the game works without a hitch, but after 4 or 5 times rebuilding my level, I am unable to use NavMesh anymore, including SetDestination, Warp and SamplePosition. My testing environment is always the same level with the same enemy placement and when I rebuild a level, I destroy all the elements (enemies with NavMeshAgent, level blocks).

    I currently have 1 NavMeshSurface which I rebuild after regenerating a level. I've tried deleting the surface and adding a new component, that didn't change anything.


    NavMesh.RemoveAllNavMeshData();

    for (int i = 0; i < surfaces.Length; i++)
    {
    surfaces[i].BuildNavMesh();
    }


    Anyone have a solution?

    Much appreciated.
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    How exactly are you "unable to use Navmesh"? What exactly is happening?
     
  3. dragon_slumber

    dragon_slumber

    Joined:
    Jun 11, 2017
    Posts:
    30
    NavMeshAgent doesn't move to Warp location, SamplePosition yields no result, that kind of stuff. It's like the NavMesh isn't there, but it definitely is and regenerating it doesn't resolve the issue.
     
  4. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    This might be a bit of a stretch, but are you certain you assign the proper Agent IDs both to the agents and to the Navmesh that is generated? It could very well be that everything functions as it should be, but because the agents that you use to sample the positions etc. have an Agent ID that does not match the NavMesh' Agent ID they have no valid Mesh to sample...
    I'm confused that it seems to work fine at first but dies after a few iterations. Have you tried reproducing this in a minimal project?
     
  5. dragon_slumber

    dragon_slumber

    Joined:
    Jun 11, 2017
    Posts:
    30
    I've not tried reproducing it out of project yet, though I might have to. Everything seems to be linked properly though and it always works the first 3-4 times. All I do is press select on the controller to regenerate the level.
     
  6. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Yeah, I'd say try and reproduce it, then if you're still having issues and you're able to (so if your code does not fall under NDA), host it somewhere so I can take a look perhaps?
     
  7. dragon_slumber

    dragon_slumber

    Joined:
    Jun 11, 2017
    Posts:
    30
    So I've since figured out something new: when my NavMeshAgents stop working, it's because they have no navMeshOwner. I've been looking for a way to assign the owner but that doesn't seem to exist. I can confirm that my scene does have a NavMesh and a Surface, I'm able to get their stats and rebuild the surface.

    Any way I can manually assign the NavMesh to the NavMeshAgent?
     
  8. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Hmmm, that's something I'm not too familiar with myself. When you log the Owner before it stops working, I assume it returns the gameobject you have your NavMesh Component on?
    It might really be as I said before, that on rebuild the Agent Type ID for the NavMesh gets lost and thus the new Mesh doesn't get assigned to the agents for use...