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;
            }
        }
    }
}

      

Link: https://github.com/datomnurdin/SurvivalShooter

+3


source to share


3 answers


You need to add Navmesh to your scene before you can use NavMeshAgent or anything else related to navigation.



Here are some Unity videos on navigation

+3


source


1. Make sure your third control controller is above ground.



2.Go to Window-> Navigation-> Bake and click with the pen. The blue area is where the third person can reach the ground.

+2


source


  • Your floor (flat or square) should be static.
  • Add your player's or your enemy's NavmeshAgent component.

You can find the static checkbox in the inspector panel

0


source







All Articles