Shared NavMesh Data across different scenes

I’ve noticed a strange bug in my game when I switch between scenes. The NavMesh surface I have in my first sandbox is overlaid in my second scene, sandbox 2. In the main menu I have both the NavMesh data from the first and second scene, and in these two scenes I can only get rid of it with this line of code, so all the data:


There is no overlapping in the first scene.
If I try to re-bake the surface from scratch in all scenes, it runs fine on the first run, there is no problem with it, but as soon as I play it again, I reproduce the error this way:

This is the Main Menu:

This is the Sandbox 2:

I’m grateful for any help

A few questions:

  • Which version of Unity are you using?
  • Are you using the new NavMeshTools (AINavigation package) or the legacy NavMesh?

Thank you for the response.

In regards to your questions I am using 2021.3.23f1, which has LTS, and I am using the legacy NavMesh, which is one that comes built in

I do recall a number of bug issues in 2021.3.23 - .26 or so (not this specific bug, but so many that I used to joke that for those versions LTS stood for Lacking Technical Stability…

Before doing anything, create a new scene, but save the scene with a new name before creating a terrain and NavMesh. Create a new terrain and set up the scene as normal. It doesnt’ have to be fancy, it needs a Portal and enough distinctiveness to make it unique.
Then create another new scene and before creating a terrain and NavMesh, give the scene a new and different name. A portal back to the first new scene will be needed here as well.

Next, temporarily remove the original scenes from the Build settings and add the two new scenes. We’re looking to see if the new scenes pick up the bug.

If that doesn’t do the trick, try updating the project to the latest 2021.3 LTS (2021.3.37).

Hahah, that’s a good one, but I don’t think that’s what caused the problem. I have updated my editor to a newer version (2022.3.23f1), so I have tried both of your suggestions but the problem still persisted…

But think about it this way, each scene in Unity is looking for NavMesh Data, and if it finds it, the NavMeshAgent will use it for navigation. So I took a look at this data and found this Agent Type ID.

Turns out that the Agent Type ID works together in both the NavMesh Data and the NavMesh Agent component to determine which NavMesh a game object can run on.
Previously they shared the same ID, so I have tried to differentiate them, but this did not resolve the issue either

So with the two completely new scenes loaded, they’re both picking up the other NavMeshData? I have NO IDEA why that would be. I suspect you’re going to have to send a bug report to Unity about this.

Well, I started a new project to test if the problem existed there as well and it didn’t.
The only difference was that I had the Enter play mode options enabled in the build settings. Unchecking it again fixed the problem which will reset the static variables before entering Play Mode, which must mean that the NavMesh data gets stored in both the .asset file and the static variable…

Oh, I hadn’t even thought of that one. I generally recommend not using the Enter Play Mode Options because it breaks Singletons in general and our own PersistentObjectSpawner as well without additional code. Good job getting that sorted! I’ll have to remember that if it comes up again.

Well yeah, just want to speed up things if possible, but quality takes time :sweat_smile:

Yeah me too. Disabling these options can help prevent data persistence across scenes due to static variables, I hadn’t thought of that before.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms