diff --git a/Assets/CustomNavMesh/Scripts/CustomNavMeshAgent.cs b/Assets/CustomNavMesh/Scripts/CustomNavMeshAgent.cs
index 50db987..db774dd 100644
--- a/Assets/CustomNavMesh/Scripts/CustomNavMeshAgent.cs
+++ b/Assets/CustomNavMesh/Scripts/CustomNavMeshAgent.cs
@@ -543,7 +543,7 @@ protected override void OnCustomDestroy()
void TryCreatingHiddenAgent()
{
- if (HiddenAgent == null)
+ if (HiddenAgent == null || !HiddenAgent.IsLinkedWith(this))
{
var hiddenObject = new GameObject("(Hidden) " + name);
hiddenObject.hideFlags = HideFlags.NotEditable;
diff --git a/Assets/CustomNavMesh/Scripts/HiddenNavMeshAgent.cs b/Assets/CustomNavMesh/Scripts/HiddenNavMeshAgent.cs
index e13fe44..a13e058 100644
--- a/Assets/CustomNavMesh/Scripts/HiddenNavMeshAgent.cs
+++ b/Assets/CustomNavMesh/Scripts/HiddenNavMeshAgent.cs
@@ -212,6 +212,16 @@ public void LinkWithCustomAgent(CustomNavMeshAgent customAgent)
TrySubscribe();
}
+ ///
+ /// Checks if this hidden agent is linked with the specified custom agent.
+ ///
+ /// The custom agent.
+ /// True if the specified agent is indeed the linked one; otherwise, false.
+ public bool IsLinkedWith(CustomNavMeshAgent customAgent)
+ {
+ return CustomAgent == customAgent;
+ }
+
protected override void OnCustomEnable()
{
var meshFilter = GetComponent();