Version: Unity 6.5 (6000.5)
LanguageEnglish
  • C#

NavWorld.IsValid

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 bool IsValid();

Returns

bool true if the NavWorld has been properly initialized, or false if the underlying navigation system has been destroyed.

Description

Checks whether the NavWorld has been properly initialized.

The only way to obtain the single possible valid NavMesh world is through a call to NavWorld.GetDefaultWorld. A NavWorld becomes invalid after a call to NavMesh.RemoveAllNavMeshData destroys the underlying navigation data. Use this method before you invoke any other NavWorld operation, to avoid exceptions that Unity throws in the Editor.

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

public class IsValidWorldExample : MonoBehaviour
{
    void Update()
    {
        using NavWorld world = NavWorld.GetDefaultWorld();
        if (!world.IsValid())
        {
            Debug.LogWarning("No valid NavMesh world is currently available.");
            return;
        }

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

Declaration

public bool IsValid(NavNode node);

Parameters

Parameter Description
node The identifier of the NavMesh node to check.

Returns

bool true if the node referenced by the specified NavNode is active in the NavMesh. Otherwise, false.

Description

Checks whether the node referenced by the specified NavNode is active in the NavMesh.

NavMesh nodes become invalid when Unity removes the NavMesh surface or the links they belong to, or when a modification of the NavMesh in their region replaces them. Calls to NavMesh.RemoveNavMeshData or NavMesh.RemoveLink remove NavMesh surfaces and links. Calls to NavMeshBuilder.UpdateNavMeshData, or carving the NavMesh with a NavMeshObstacle, modify the NavMesh.

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

public class IsValidNodeExample : MonoBehaviour
{
    NavNode m_CachedNode;

    void Start()
    {
        using NavWorld world = NavWorld.GetDefaultWorld();
        NavLocation location = world.MapLocation(transform.position, Vector3.one, 0);
        m_CachedNode = location.node;
    }

    void Update()
    {
        using NavWorld world = NavWorld.GetDefaultWorld();
        if (!world.IsValid(m_CachedNode))
            Debug.LogWarning("Cached node was removed or replaced; re-map the location.");
    }
}

Declaration

public bool IsValid(NavLocation location);

Parameters

Parameter Description
location The location on the NavMesh to check. Checking the location is the same as checking location.node directly.

Returns

bool true if the node referenced by the NavNode contained in the NavLocation is active in the NavMesh. Otherwise, false.

Description

Checks whether the node referenced by the NavNode contained in the NavLocation is active in the NavMesh.

A NavLocation returned by NavWorld.MapLocation can become invalid if the NavMesh data changes around its position, for example through carving by a NavMeshObstacle or a NavMeshBuilder.UpdateNavMeshData call. Verify the location before passing it to other NavWorld operations.

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

public class IsValidLocationExample : MonoBehaviour
{
    public Transform target;

    void Update()
    {
        using NavWorld world = NavWorld.GetDefaultWorld();
        NavLocation location = world.MapLocation(target.position, Vector3.one, 0);
        if (!world.IsValid(location))
            return;

        Debug.DrawLine(transform.position, location.position, Color.cyan);
    }
}