Version: Unity 6.5 (6000.5)
LanguageEnglish
  • C#

NavWorld.GetDefaultWorld

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public static NavWorld GetDefaultWorld();

Returns

NavWorld A reference to the single NavWorld that can currently exist and be used in Unity.

Description

Obtains a reference to the navigation mesh world where all navigation operations occur.

The returned world comprises all the NavMesh surfaces and connections that are also used through the NavMesh-related structures.

The world becomes invalid after a call to NavMesh.RemoveAllNavMeshData. A new call to GetDefaultWorld returns a world that is different than the one that has been destroyed.

Each call returns a separate handle that holds resources of its own. Pair every call with a NavWorld.Dispose call, for example through a using declaration, otherwise the handles accumulate for as long as the navigation data exists.

Additional resources: NavWorld.IsValid, NavWorld.Dispose, NavMesh.AddNavMeshData, NavMesh.AddLink

using UnityEngine;
using Unity.AI.Navigation.LowLevel;

public class GetDefaultWorldExample : MonoBehaviour
{
    NavWorld m_World;

    void OnEnable()
    {
        // Each handle owns a safety handle, so obtain one here and keep
        // it for as long as you run queries, instead of asking for a new
        // handle on every frame.
        m_World = NavWorld.GetDefaultWorld();
    }

    void OnDisable()
    {
        m_World.Dispose();
    }

    void Update()
    {
        if (!m_World.IsValid())
            return;

        NavLocation location =
            m_World.MapLocation(transform.position, 100f * Vector3.one, 0);
        if (m_World.IsValid(location))
            Debug.DrawLine(transform.position, location.position, Color.green);
    }
}