Search Unity

GetComponent<NavMeshAgent> does not work on all

Discussion in 'Navigation' started by wolfomat, Apr 20, 2020.

  1. wolfomat

    wolfomat

    Joined:
    Oct 19, 2014
    Posts:
    24
    Hi there,

    I am not sure what my issue is, here is the description of my problem:

    I've created a helper script for nav-mesh-navigation, one one GameObject everything's fine and at the other it's not working.

    My Script:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.AI;
    4.  
    5. namespace DoNotWork.scripts.click
    6. {
    7.     public class HumanAgent : MonoBehaviour
    8.     {
    9.         // navigation
    10.         private NavMeshAgent navMeshAgent;
    11.         public Transform[] navigationPoints;
    12.  
    13.         // animation
    14.         // private Animator animator;
    15.  
    16.         // states
    17.         private bool isReadyForWalk;
    18.         private static readonly int IsWalking = Animator.StringToHash("isWalking");
    19.  
    20.         void Start()
    21.         {
    22.             navMeshAgent = GetComponent<NavMeshAgent>();
    23.             // animator = GetComponent<Animator>();
    24.             isReadyForWalk = true;
    25.            
    26.             navMeshAgent.enabled = true;
    27.         }
    28.  
    29.         public void GotoStall(int stallSelection)
    30.         {
    31.             isReadyForWalk = false;
    32.             navMeshAgent.destination = navigationPoints[stallSelection].position;
    33.             // animator.SetBool(IsWalking, true);
    34.         }
    35.  
    36.         public void WarpAgent(Vector3 position)
    37.         {
    38.             navMeshAgent.Warp(position);
    39.             // animator.SetBool(IsWalking, false);
    40.         }
    41.  
    42.         public void StopAnimation()
    43.         {
    44.             // animator.SetBool(IsWalking, false);
    45.         }
    46.  
    47.         public bool IsReadyForWalk
    48.         {
    49.             get { return isReadyForWalk; }
    50.         }
    51.  
    52.         public void SetReady()
    53.         {
    54.             isReadyForWalk = true;
    55.         }
    56.     }
    57. }
    Error:

    NullReferenceException: Object reference not set to an instance of an object
    DoNotWork.scripts.click.HumanAgent.GotoStall (Int32 stallSelection) (at Assets/DoNotWork/scripts/click/HumanAgent.cs:32)
    SpawnNavigationManager.Spawn () (at Assets/DoNotWork/scripts/click/SpawnNavigationManager.cs:88)
    DoNotWork.scripts.click.ClickScript.Update () (at Assets/DoNotWork/scripts/click/ClickScript.cs:77)


    Where navMeshAgent is null on the broken one.
    I've added two screenshots with the working one and the broken one.

    strange part is, i've several other gameObjects where this is working but the "broken" one will simply not work..... Start() is called.. ?? any ideas?
     

    Attached Files:

  2. wolfomat

    wolfomat

    Joined:
    Oct 19, 2014
    Posts:
    24
    and here the NavMeshAgent components:

    broken_agent.JPG working_agent.JPG
     
  3. wolfomat

    wolfomat

    Joined:
    Oct 19, 2014
    Posts:
    24
    can be closed. there was another script involved which made these broken Oo