Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Swapping navMeshOwner between scenes at runtime

Discussion in 'Navigation' started by Kappatrackies, Jan 22, 2023.

  1. Kappatrackies

    Kappatrackies

    Joined:
    Apr 17, 2017
    Posts:
    1
    In my current project, the player object uses a NavMeshAgent to move around. When I swap scenes, I'm persisting the player object using SceneManager.MoveGameObjectToScene while loading the new scene asynchronously, and unloading the old scene.

    The issue I'm facing is that when the new scene loads in with a new NavMesh, the player's NavMeshAgent is not bound to the new NavMesh. When I call NavMeshAgent.navMeshOwner it returns null, I assume this is because the old terrain and NavMesh have been unloaded.

    Is there any way for me to manually bind the player object to the new NavMesh? NavMeshAgent.navMeshOwner is a static field, so I'm unable to just update it through script. I've done a fair bit of reading, and it looks like OffMeshLinks are my only option, but it seems that the NavMeshAgent needs to actually walk over those. Any suggestions would be appreciated.

    TLDR: How can I manually bind a NavMeshAgent to a specific NavMesh?

    Thanks.
     
  2. androvisuals_unity

    androvisuals_unity

    Joined:
    Mar 23, 2020
    Posts:
    47
    This sounds like a very difficult way to do it.
    I would just instantiate a new copy of the navagent "after" the loading is done. When it's instantiated then it will automatically be associated with the new navmesh.
    You may have to update the "prefab" and then create it in one frame but this won't be noticeable to the player if you do it correctly.