Enabling and Disabling Components & NavMeshLink Type Not Recognized

I have this script which is supposed to disable or enable a NavMeshLink component whenever a bool called “open” in a gate script is set to true or false, but the editor doesn’t recognize the NavMeshLink type in the script. Also, when I tried to separate the enabling and disabling of the NavMeshLink, it returned an error saying that I cannot enable or disable a component the way that I was trying. Is there anyway that I can get this script to work so that it recognizes the NavMeshLink type and according enables or disables the NavMeshLink component?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class GateToggle : MonoBehaviour
{
    [Header("References")]
    public Gate gate;
    public NavMeshLink link;

    private void Update()
    {
        if (link != null && gate != null)
        {
            link.enabled = gate.open;
        }
        else if (link != null && gate == null)
        {
            link.enabled = true;
        }
    }
}

Since I couldn’t fix this issue what I did instead was to put a NavMesh Obstacle on the gate itself so that agents can’t walk through it. The reason that I was trying to disable and enable the NavMeshLink in the first place was to have agents not walk through the gate if it was closed, but making the gate itself a NavMesh Obstacle has seemed to fix that issue itself without the need of any code (not including the code to run animations for the gate).

The NavMeshLink component is declared in the Unity.AI.Navigation namespace. Add using Unity.AI.Navigation; at the beginning of the script so that the component can be recognized in the script.
As an alternative, you can also use it at line 10 with the fully qualified name:

public Unity.AI.Navigation.NavMeshLink link;

This applies if your project uses the AI Navigation package.
Documentation: Class NavMeshLink | AI Navigation | 2.0.3

1 Like