GetRemainingDistance can only be called on an active agent that has been placed on a NavMesh

TheLost Lostit picture TheLost Lostit · Jul 23, 2016 · Viewed 7k times · Source

I have two errors:

The first error is:

MissingComponentException: There is no 'NavMeshAgent' attached to the "ThirdPersonController" game object, but a script is trying to access it. You probably need to add a NavMeshAgent to the game object "ThirdPersonController". Or your script needs to check if the component is attached before using it.

Patroll.Update () (at Assets/My Scripts/Patroll.cs:41)

The Patroll.Update is in a script file I created called: Patroll.cs

using UnityEngine;
using System.Collections;

public class Patroll : MonoBehaviour {

    public Transform[] points;
    private int destPoint = 0;
    private NavMeshAgent agent;

    // Use this for initialization
    void Start () {

        agent = GetComponent<NavMeshAgent>();

        // Disabling auto-braking allows for continuous movement
        // between points (ie, the agent doesn't slow down as it
        // approaches a destination point).
        agent.autoBraking = false;

        GotoNextPoint();

    }

    void GotoNextPoint() {
        // Returns if no points have been set up
        if (points.Length == 0)
            return;

        // Set the agent to go to the currently selected destination.
        agent.destination = points[destPoint].position;

        // Choose the next point in the array as the destination,
        // cycling to the start if necessary.
        destPoint = (destPoint + 1) % points.Length;
    }


    void Update () {
        // Choose the next destination point when the agent gets
        // close to the current one.
        if (agent.remainingDistance < 0.5f)
            GotoNextPoint();
    }
}

Line 41 is:

if (agent.remainingDistance < 0.5f)

This script Patroll.cs I dragged over to Hierarchy to ThirdPersonController.

Then after this I have another error and this error I also had even before I created the Patroll.cs script:

"GetRemainingDistance" can only be called on an active agent that has been placed on a NavMesh. UnityEngine.NavMeshAgent:get_remainingDistance() UnityStandardAssets.Characters.ThirdPerson.AICharacterControl:Update() (at Assets/Standard Assets/Characters/ThirdPersonCharacter/Scripts/AICharacterControl.cs:31)

This error is in the script AICharacterControl.cs it's unity script and also related to the ThirdPersonController in the Hierarchy.

Line 31:

if (agent.remainingDistance > agent.stoppingDistance)

What I tried to do so far to fix it is in unity. I clicked on the menu on Component > Navigation > NavMesh Agent

Now it added to the ThirdPersonController the Nav Nesh Agent and I can see in the Inspector of ThirdPersonController the Nav Nesh Agent part.

But the error/s still exist.

This is the AICharacterControl.cs script

using System;
using UnityEngine;

namespace UnityStandardAssets.Characters.ThirdPerson
{
    [RequireComponent(typeof (NavMeshAgent))]
    [RequireComponent(typeof (ThirdPersonCharacter))]
    public class AICharacterControl : MonoBehaviour
    {
        public NavMeshAgent agent { get; private set; }             // the navmesh agent required for the path finding
        public ThirdPersonCharacter character { get; private set; } // the character we are controlling
        public Transform target;                                    // target to aim for


        private void Start()
        {
            // get the components on the object we need ( should not be null due to require component so no need to check )
            agent = GetComponentInChildren<NavMeshAgent>();
            character = GetComponent<ThirdPersonCharacter>();

            agent.updateRotation = false;
            agent.updatePosition = true;
        }


        private void Update()
        {
            if (target != null)
                agent.SetDestination(target.position);

            if (agent.remainingDistance > agent.stoppingDistance)
                character.Move(agent.desiredVelocity, false, false);
            else
                character.Move(Vector3.zero, false, false);
        }


        public void SetTarget(Transform target)
        {
            this.target = target;
        }
    }
}

I can't figure out how to fix the errors.

Answer

Vu Hoang Son picture Vu Hoang Son · May 28, 2017

I got the same problem in my game. That happened when I load character prefab in running time, that prefab in my old map has different position. To fix that you can put your prefab on the navigation mesh and save your prefab.