Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Check if there is a NavMesh in the scene

Discussion in 'Navigation' started by Griffo, Dec 12, 2016.

  1. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    664
    Hi,

    I'd like to check if there is a NavMesh in the scene, I thought this code would work.

    Code (JavaScript):
    1. private var _navMesh : UnityEngine.AI.NavMesh[];
    2.  
    3.     _navMesh = FindObjectsOfType.<UnityEngine.AI.NavMesh>();
    4.  
    5.     if(_navMesh == null)
    6.     {
    7.         return;
    8.     }
    Thanks.
     
    Last edited: Dec 12, 2016
  2. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    664
    OR ..

    Code (JavaScript):
    1. _navMesh = FindOfType.<UnityEngine.AI.NavMesh>();
     
  3. michaelday008

    michaelday008

    Joined:
    Mar 29, 2019
    Posts:
    8
    I feel like there must be a simpler way to do this, but...

    NavMeshHit hit;
    if (NavMesh.SamplePosition(Vector3.zero, out hit, 1000.0f, NavMesh.AllAreas)) {
    Vector3 result = hit.position;
    navMeshAvailable = true;
    } else {
    navMeshAvailable = false;
    }

    This seems to work for me. It's kind of hacky because I think it may be possible that there is some circumstance where you have a huge scene with no NavMesh within 1000 meters of Vector3.zero.

    The use case is avoiding that warning you get when you try to enable a NavMeshAgent on a scene with no NavMesh baked yet: "Failed to create agent because there is no valid NavMesh"

    I have a character controller that can use NavMesh Agents or standard transform velocity, and I want it to autodetect which method it should use on scene load based on whether or not it can find a NavMesh in the scene.
     
    Last edited: Jun 30, 2019