NavMesh Error When killing an ennemy

HI, when i kill an enemy the game crashes and i get this error:

“Stop” can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.StackTraceUtility:ExtractStackTrace ()

here is my code from Mover:

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

namespace Rpg.Movement
{
    public class Mover : MonoBehaviour,IAction
    {
        [SerializeField] private Transform target;
        [SerializeField] private float maxSpeed = 6f;

        NavMeshAgent navMeshAgent;
        Health health;
        // Start is called before the first frame update
        void Start()
        {
            health= GetComponent<Health>(); 
            navMeshAgent= GetComponent<NavMeshAgent>();
        }

        // Update is called once per frame
        void Update()
        {
            navMeshAgent.enabled = !health.isDead;
            UpdateAnimator();
        }

        private void UpdateAnimator()
        {
            Vector3 velocity = GetComponent<NavMeshAgent>().velocity;
            Vector3 localVelocity = transform.InverseTransformDirection(velocity);
            float speed = localVelocity.z;
            GetComponent<Animator>().SetFloat("fowardSpeed", speed);
        }
        public void Cancel()
        {
            navMeshAgent.isStopped= true;
        }
        public void MoveTo(Vector3 destination, float speedFraction)
        {
            navMeshAgent.isStopped = false;
            navMeshAgent.speed = maxSpeed * Mathf.Clamp01(speedFraction);
           navMeshAgent.destination = destination;
        }
        public void StartMoveAction(Vector3 destination,float speedFraction)
        {
            GetComponent<ActionScheduler>().StartAction(this);
            MoveTo(destination,speedFraction);
        }

    
    }

}

Here is my fighter code :

using Rpg.Core;
using Rpg.Movement;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Rpg.Combat
{
    public class Fighter : MonoBehaviour, IAction
    {
        
        [SerializeField] Weapon defaultWeapon = null;
        [SerializeField] Transform handTransform = null;

        private Health target;
        float timeSinceLastAttack = Mathf.Infinity;
        Weapon currentWeapon= null;

        private void Start()
        {
            EquipWeapon(defaultWeapon);
        }



        private void Update()
        {
            timeSinceLastAttack += Time.deltaTime;
            if (target == null) return;
            if (target.isDead) return;

            if (!GetIsInRange())
            {
                GetComponent<Mover>().MoveTo(target.transform.position, 1f);
            }
            else
            {
                GetComponent<Mover>().Cancel();
                AttackBehavior();
            }
        }
        public void EquipWeapon(Weapon weapon)
        {
            currentWeapon= weapon;  
            Animator animator = GetComponent<Animator>();
            weapon.Spawn(handTransform,animator);

        }

        private void AttackBehavior()
        {
            transform.LookAt(target.transform);
            if (timeSinceLastAttack > currentWeapon.GetTimeBetweenAttack())
            {
                //trigger hit event
                TriggerAttack();
                timeSinceLastAttack = 0f;

            }

        }

        private void TriggerAttack()
        {
            GetComponent<Animator>().ResetTrigger("stopAttack");
            GetComponent<Animator>().SetTrigger("attack");
        }

        //Animation event
        void Hit()
        {
            if (target == null) return;
            target.TakeDamage(currentWeapon.GetDamage());
        }
        private bool GetIsInRange()
        {
            return Vector3.Distance(transform.position, target.transform.position) < currentWeapon.GetRange();
        }

        public void Attack(GameObject combatTarget)
        {
            GetComponent<ActionScheduler>().StartAction(this);
            target = combatTarget.GetComponent<Health>();
        }
        public void Cancel()
        {
            GetComponent<Mover>().Cancel();
            StopAttack();
            target = null;
        }

        private void StopAttack()
        {
            GetComponent<Animator>().ResetTrigger("attack");
            GetComponent<Animator>().SetTrigger("stopAttack");
        }

        public bool CanAttack(GameObject combatTarget)
        {
            if (combatTarget == null) return false;
            Health targetToTest = combatTarget.GetComponent<Health>();
            return targetToTest != null && !targetToTest.isDead;
        }

    }

}

Thanks !

That is an odd one… you shouldn’t be getting a NavMesh error, as you wouldn’t even be able to get to the enemy to kill them if you weren’t on the NavMesh (and you’d have encountered said error).

I’m also not sure how you could get to the line GetComponent<Mover>().Cancel(); line if the target was null or dead…

Which version of Unity are you using? (I strongly recommend against Unity 2022.3x for the time being as it’s a bit of a train wreck, even though they call it LTS).

What happens to the enemy when you kill them?

What do you mean by Crash? Is the editor closing down, or are you just getting the NRE?

I switched to 2022.3.4 it doesnt’ crash but I get the same error

Right now, any version of 2022.3 is problematic…

That being said, I’m assuming then, that you’re using the NavMeshSurface (baked onto a GameObject within the scene)?

If that’s the case, make sure that the NavMeshSurface is not on anything that might be destroyed, like say… .an enemy…

It’s still a problem when I am on 2021.3…

Zip up your project and upload it to https://gdev.tv/projectupload
Be sure to remove the Library folder and any build folders to conserve space.

1 Like

Sent! It is my name kyle

At no point was play interrupted. I seem to have been able to kill the enemies normally.
I was getting spammed with the stop can only be called on message when a character dies.
This was coming from Mover but the root cause was Fighter which was still dutifully trying to attack.

Try adding this in Health.Die()

GetComponent<ActionScheduler>().CancelCurrentAction();

Great! It works Thanks!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms