Fix - X can only be called on an active agent that has been placed on a NavMesh

So there’s a chance you might be getting this error now depending on what you did earlier in the course.

We’re now calling Cancel on the mover script from within the fighter script. If the enemy dies, it will trigger the CancelCurrentAction() code which will eventually call Cancel on Mover. All well and good unless you happened to disable the enemy navMeshAgent on death BEFORE calling CancelCurrentAction().

It was disconcerting to see this all of a sudden but fortunately the fix is very simple. Just disable the navmesh agent after you call CancelCurrentAction(). Here’s what my code looks like.

        private void Die()
        {
            isDead = true;
            GetComponent<Animator>().SetTrigger("die");
            GetComponent<ActionScheduler>().CancelCurrentAction();
            if (TryGetComponent<Collider>(out Collider collider))
            {
                collider.enabled = false;
            }
            if (TryGetComponent<NavMeshAgent>(out NavMeshAgent navMeshAgent))
            {
                navMeshAgent.enabled = false;
            }
        }
2 Likes

Just be aware that TryGetComponent() didn’t exist yet in the 2018 releases of Unity (it was added in 2019.x) , in case you’re following the course using the older version of Unity…

Privacy & Terms