Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

"SetDestination" can only be called on an active agent that has been placed on a NavMesh.

Discussion in 'Navigation' started by Willrichplaysguitar, Apr 9, 2022.

  1. Willrichplaysguitar

    Willrichplaysguitar

    Joined:
    Dec 30, 2020
    Posts:
    1
    Having trouble with this error. I am following a tutorial and disabled the navmesh agent after a certain trigger. It worked in the tutorial, but not for me. I already have applied the navmesh and baked it into the scene, so I'm not sure why I'm still getting this. The code is below. If it's not something with the code I suspect it has something to do with the prefab.
    Thanks!

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AI;
    public class EnemyAI : MonoBehaviour
    {
    [SerializeField] Transform target;
    [SerializeField] float chaseRange = 5f;
    [SerializeField] float turnSpeed = 3f;
    EnemyHealth enemyHealth;
    float distanceToTarget = Mathf.Infinity;
    NavMeshAgent agent;

    bool isProvoked;
    // Start is called before the first frame update
    void Start()
    {
    agent = GetComponent<NavMeshAgent>();
    enemyHealth = GetComponent<EnemyHealth>();
    }
    // Update is called once per frame
    void Update()
    {
    if (enemyHealth.IsDead())
    {
    enabled = false;
    agent.enabled = false;
    }
    distanceToTarget = Vector3.Distance(target.position, transform.position);
    if (isProvoked)
    {
    EngageTarget();
    }
    else if (distanceToTarget <= chaseRange)
    {
    isProvoked = true;
    }
    }
    public void OnDamageTaken()
    {
    isProvoked = true;
    }
    void EngageTarget()
    {
    FaceTarget();
    if (distanceToTarget >= agent.stoppingDistance)
    {
    ChaseTarget();
    }
    if (distanceToTarget <= agent.stoppingDistance)
    {
    AttackTarget();
    }
    }
    void ChaseTarget()
    {
    GetComponent<Animator>().SetBool("attack", false);
    GetComponent<Animator>().SetTrigger("move");
    agent.SetDestination(target.position);
    }

    void AttackTarget()
    {
    GetComponent<Animator>().SetBool("attack", true);
    print("Attacking Target");
    }
    private void FaceTarget()
    {
    Vector3 direction = (target.position - transform.position).normalized;
    Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
    transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * turnSpeed);
    }
    private void OnDrawGizmos()
    {
    Gizmos.color = Color.red;
    Gizmos.DrawWireSphere(transform.position, chaseRange);
    }
    }

    *Another Script Linking to this one below

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class EnemyHealth : MonoBehaviour
    {
    [SerializeField] float hitPoints = 100f;
    bool isDead = false;
    public bool IsDead()
    {
    return isDead;
    }
    public void TakeDamage(float damage)
    {
    BroadcastMessage("OnDamageTaken");
    hitPoints -= damage;
    if(hitPoints <= 0)
    {
    Die();
    }
    }
    public void Die()
    {
    if (isDead) return;
    isDead = true;
    GetComponent<Animator>().SetTrigger("die");
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,936