IsValid
Checks whether the NavWorld has been properly initialized.
Read time 2 minutesLast updated 21 days ago
Definition
- Type: Method
- Namespace: Unity.AI.Navigation.LowLevel
- Assembly: UnityEngine.AIModule
IsValid()
Checks whether the NavWorld has been properly initialized.
public readonly bool IsValid()
Returns
Type | Description |
|---|---|
| bool | |
Remarks
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.
Examples
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); }}
IsValid(NavNode)
Checks whether the node referenced by the specified NavNode is active in the NavMesh.
public readonly bool IsValid(NavNode node)
Parameters
The identifier of the NavMesh node to check.
Returns
Type | Description |
|---|---|
| bool | |
Remarks
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 UpdateNavMeshData, or carving the NavMesh with a NavMeshObstacle, modify the NavMesh.
Examples
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."); }}
IsValid(NavLocation)
Checks whether the node referenced by the NavNode contained in the NavLocation is active in the NavMesh.
public readonly bool IsValid(NavLocation location)
Parameters
The location on the NavMesh to check. Checking the location is the same as checking directly.
location.nodeReturns
Type | Description |
|---|---|
| bool | |
Remarks
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 UpdateNavMeshData call. Verify the location before passing it to other NavWorld operations.
Examples
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); }}