Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

I'm getting error: “GetRemainingDistance” can only be called on an active agent how to fix?

Discussion in 'Scripting' started by Chocolade, Jul 23, 2016.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    I have two errors:

    The first erorr 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

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. publicclassPatroll:MonoBehaviour{
    6.  
    7. publicTransform[] points;privateint destPoint =0;privateNavMeshAgent agent;
    8.  
    9. // Use this for initializationvoidStart(){
    10.  
    11. agent =GetComponent<NavMeshAgent>();
    12.  
    13. // Disabling auto-braking allows for continuous movement// between points (ie, the agent doesn't slow down as it// approaches a destination point).
    14. agent.autoBraking =false;
    15.  
    16. GotoNextPoint();
    17.  
    18. }
    19.  
    20. voidGotoNextPoint(){// Returns if no points have been set upif(points.Length==0)return;
    21.  
    22. // Set the agent to go to the currently selected destination.
    23. agent.destination = points[destPoint].position;
    24.  
    25. // Choose the next point in the array as the destination,// cycling to the start if necessary.
    26. destPoint =(destPoint +1)% points.Length;}
    27.  
    28.  
    29. voidUpdate(){// Choose the next destination point when the agent gets// close to the current one.if(agent.remainingDistance <0.5f)GotoNextPoint();}}
    Line 41 is:

    Code (csharp):
    1.  
    2. 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 ot the ThirdPersonController in the Hierarchy.

    Line 31:

    Code (csharp):
    1. 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 > Nav Mesh 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

    Code (csharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4.  
    5. namespace UnityStandardAssets.Characters.ThirdPerson{[RequireComponent(typeof(NavMeshAgent))][RequireComponent(typeof(ThirdPersonCharacter))]publicclassAICharacterControl:MonoBehaviour{publicNavMeshAgent agent { get;privateset;}// the navmesh agent required for the path findingpublicThirdPersonCharacter character { get;privateset;}// the character we are controllingpublicTransform target;// target to aim for
    6.  
    7.  
    8. privatevoidStart(){// get the components on the object we need ( should not be null due to require component so no need to check )
    9. agent =GetComponentInChildren<NavMeshAgent>();
    10. character =GetComponent<ThirdPersonCharacter>();
    11.  
    12. agent.updateRotation =false;
    13. agent.updatePosition =true;}
    14.  
    15.  
    16. privatevoidUpdate(){if(target !=null)
    17. agent.SetDestination(target.position);
    18.  
    19. if(agent.remainingDistance > agent.stoppingDistance)
    20. character.Move(agent.desiredVelocity,false,false);else
    21. character.Move(Vector3.zero,false,false);}
    22.  
    23.  
    24. publicvoidSetTarget(Transform target){this.target = target;}}}
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Whathappentoyourspacing?

    Ok, in all seriousness, this is hard to read. I'm not sure where all your spacing went, but whatever happen to it, you should fix that when sharing code. Also helps to keep lines separate for readability. I'm not sure how your namespace, requiredcomponent, and class name are all on one line.

    Now, the only thing I see off hand is your GetComponentInChildren call. Is the navMeshAgent in a child of the object that has this script attached or is it on the same object?
     
  3. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    Sorry i will fix the main post.

    The navMeshAgent i just added to the ThirdPersoncCntroller so i guess it's the same object not a child.
     
  4. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916

    You can see the Nav Mesh Agent where i added it. It's inside the ThirdPersonController where also the Patroll script.

     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Since the navmesh and your scripts are on the same object, it should work with getcomponent and not getComponentInChildren. Is it not working for you?
     
  6. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    No it's not working. I remove my script Patroll.cs from the ThirdPersonController/ Then i tried in the script where the error is show to change it from getComponentInChildren to getcomponent but still same erorr.
    The error is not on the ThirdPersonControll script and not in the Patroll.cs script i did. The errorr in the script that come with unity in the file: AICharacterControl.cs at line 31.

    Code (csharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4.  
    5. namespace UnityStandardAssets.Characters.ThirdPerson
    6. {
    7.     [RequireComponent(typeof (NavMeshAgent))]
    8.     [RequireComponent(typeof (ThirdPersonCharacter))]
    9.     public class AICharacterControl : MonoBehaviour
    10.     {
    11.         public NavMeshAgent agent { get; private set; }             // the navmesh agent required for the path finding
    12.         public ThirdPersonCharacter character { get; private set; } // the character we are controlling
    13.         public Transform target;                                    // target to aim for
    14.  
    15.  
    16.         private void Start()
    17.         {
    18.             // get the components on the object we need ( should not be null due to require component so no need to check )
    19.             agent = GetComponentInChildren<NavMeshAgent>();
    20.             character = GetComponent<ThirdPersonCharacter>();
    21.  
    22.            agent.updateRotation = false;
    23.            agent.updatePosition = true;
    24.         }
    25.  
    26.  
    27.         private void Update()
    28.         {
    29.             if (target != null)
    30.                 agent.SetDestination(target.position);
    31.  
    32.             if (agent.remainingDistance > agent.stoppingDistance)
    33.                 character.Move(agent.desiredVelocity, false, false);
    34.             else
    35.                 character.Move(Vector3.zero, false, false);
    36.         }
    37.  
    38.  
    39.         public void SetTarget(Transform target)
    40.         {
    41.             this.target = target;
    42.         }
    43.     }
    44. }
    The error is on the line:

    Code (csharp):
    1. if (agent.remainingDistance > agent.stoppingDistance)
    I have on my Hierarchy two characters: AIThirdPersonController and ThirdPersonController.
    Main Camera also Terrain.

    And the error in this file script was before i added Nav Mesh Agent to the ThirdPersonController before i added my script Patroll.cs to the ThirdPersonController.

    This is a link to my project a winrar file the name is My First Game.rar https://1drv.ms/u/s!AtV2OUzEcRzrlm3-x-B607LxMNOw maybe somenoe can look at it see the error.
     
  7. Rebalgames786

    Rebalgames786

    Joined:
    Oct 31, 2017
    Posts:
    2
    Bake Your Environment And Try Agian
     
    SatyanshuDevEther and gaiu01999 like this.
  8. weaglf

    weaglf

    Joined:
    Nov 1, 2016
    Posts:
    4
    I fixed mine by increasing radius of nav mesh agent.
     
  9. adibichea

    adibichea

    Joined:
    Jul 15, 2015
    Posts:
    73
    I had the same problem and fixed with
    Code (CSharp):
    1. if (!navMeshAgent.isOnNavMesh)
    2.             return;
    at the beginning of the function.

    This problem appears when we instantiate a prefab and place it to the navmesh.
     
    KSF, clamum and twinklemc91 like this.
  10. windbylocus

    windbylocus

    Joined:
    Aug 1, 2018
    Posts:
    5
    THANKS!
     
  11. clamum

    clamum

    Joined:
    May 14, 2017
    Posts:
    61
    You rock! I was getting the "remainingDistance can only be called on an active agent" when spawning my prefabs into the game and having them go to a random location, then once they reach it, go to another random location and repeat that.

    That little check at the beginning of my "HasPersonReachedDestination()" method appears to have fixed it. Thanks again!

    PS: For anyone who's interested, here's my method to check if an agent has reached their destination (absolutely ridiculous this isn't a built-in method or implemented via callbacks, but whatever):

    Code (CSharp):
    1. public bool HasNavMeshAgentReachedDestination()
    2.     {
    3.         // need to check first if the person is spawned and on the navmesh
    4.         if (!NavMeshAgent.isOnNavMesh)
    5.         {
    6.             return false;
    7.         }
    8.  
    9.         if (!NavMeshAgent.pathPending)
    10.         {
    11.             if (NavMeshAgent.remainingDistance <= NavMeshAgent.stoppingDistance)
    12.             {
    13.                 if (!NavMeshAgent.hasPath || NavMeshAgent.velocity.sqrMagnitude == 0f)
    14.                 {
    15.                     return true;
    16.                 }
    17.             }
    18.         }
    19.  
    20.         return false;
    21.     }
     
    owlrazum likes this.
  12. rogodoy

    rogodoy

    Joined:
    Jan 24, 2013
    Posts:
    21
    If you move the player at runtime or by spawning the player in the navmesh that was baked, since it was baked in different position, you will have this error. To overcome this problem just bake again the navmesh. But the solution of clamum is good for solve the problem in runtime. I have got this error because i moved the player from baked player position to a new position. Whem I baked again the player and the navmesh this error did not occurred.
     
    CalmyRaindrops and binarywritter like this.
  13. Leohd

    Leohd

    Joined:
    Feb 7, 2017
    Posts:
    32
    You can also add an if check using the NavMeshAgent.isOnNavMesh function. Also check where you're spawning your dudes, make sure you spawn them slightly above the spawning point.
     
  14. bartiggro

    bartiggro

    Joined:
    Apr 4, 2020
    Posts:
    1
    check if it can move
     
  15. Vice69

    Vice69

    Joined:
    Mar 28, 2023
    Posts:
    8
    Make sure that you baked your floor with Navigation Static "Walkable"