Unity - "SetDestination" can only be called by an active agent that has been placed in the NavMesh. UnityEngine.NavMeshAgent: SetDestination (Vector3)
I am using Unity5 right now. I got this error when trying to set destination.
"SetDestination" can only be called for an active agent that has been placed in the NavMesh. UnityEngine.NavMeshAgent: SetDestination (Vector3) CompleteProject.EnemyMovement: Update () (under Assets / _CompletedAssets / Scripts / Enemy / EnemyMovement.cs: 30)
My code
using UnityEngine;
using System.Collections;
namespace CompleteProject
{
public class EnemyMovement : MonoBehaviour
{
Transform player; // Reference to the player position.
PlayerHealth playerHealth; // Reference to the player health.
EnemyHealth enemyHealth; // Reference to this enemy health.
NavMeshAgent nav; // Reference to the nav mesh agent.
void Awake ()
å{
// Set up the references.
player = GameObject.FindGameObjectWithTag ("Player").transform;
playerHealth = player.GetComponent <PlayerHealth> ();
enemyHealth = GetComponent <EnemyHealth> ();
nav = GetComponent <NavMeshAgent> ();
}
void Update ()
{
// If the enemy and the player have health left...
if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
{
// ... set the destination of the nav mesh agent to the player.
nav.SetDestination (player.position);
}
// Otherwise...
else
{
// ... disable the nav mesh agent.
nav.enabled = false;
}
}
}
}
+3
source to share