Rebuilding dynamic navmesh via c++ stops working after switching levels

I use simple c++ to rebuild navmesh at runtime when the level changes. This works no problem. However after I load any other level containing a navmesh and return to this one, the code stops working. There are no error logs, it simply generates an empty navmesh and displays a log that building took 0.0 seconds.

UNavigationSystemV1* NavSystem = UNavigationSystemV1::GetCurrent(GetWorld());
if (NavSystem->IsInitialized())
{
	NavMeshBounds->SetActorLocation(SomeOtherLocation); // move bounds to new location
	NavigationSystemV1->Build(); //build the nav mesh
        //this works when this is the first level loaded, after i load a different one and return it breaks
}

Any help would be greatly appreciated!

EDIT: I’m using Unreal 5.2

Hey @kitun, could you share a few more details?

The code you’re showing—where is it exactly? Is it in the BeginPlay of an actor or somewhere else?
What configuration do you have for the NavMesh? I assume it’s dynamic (based on the title), but just to confirm.

Does the other level already contain a RecastNavMesh actor?

Also, why are you rebuilding it manually if the dynamic NavMesh should automatically rebuild when the geometry or level changes?

Hi, thanks for reaching out!

I use the code whenever I add a tile to the level (50x50 meters approx) Its a very simple function that makes sure the level never ends pic below:


Example:

  1. Player reaches tile 2
  2. Add tiles A,B,C
  3. Move navmesh bounds to tile 2 (the bounding box is bigger than the tile)
  4. Rebuild navmesh

The navmesh is dynamic, and yes both levels contain the RecastNavMeshActor

I move the bounding box because the level has no limits and can grow infinitely.

@kitun All right, are you instancing the tile (level instancing), or are you loading it as a sublevel?

Can I see the code where you’re calling the navmesh rebuild? Are you waiting for everything to load/show before triggering the rebuild?

The tiles are simple actors, I’m adding them to the world via the GetWorld()->SpawnActor method. There is no sublevels or level instancing.

The code that calls for navmesh generation is just what i sent in the OP, there’s nothing more to it. Simply move the navmesh bounding volume and then call build on the navigation system.

About the method calls, they are called after some time of gameplay, only when player reaches certain triggers, so I’m pretty sure that it’s not something not being initialized.

The thing is, this works without issue if this is the first loaded level, so I’m guessing the logic works ok. It stops working only when i load a different level with a navmesh and return to it. (Both navmeshes have the same dynamic configuration btw).