Navmeshagent is null

MarcOrion

New member
So I started using behavior designer (with movement pack) yesterday and it seems straight forward but when I do a simple behavior where I try to move/set destination I get the NavMeshAgent is null error. When I attach a test script with a simple GetComponent<NavMeshAgent>() it gets it just fine, but when running the behavior tree it will not get the navmeshagent. See attached:

behavior tree error.PNG

Both the behavior and my test script are on the same object, so I dont see why one works and the other doesnt. Any help would be appreciated. Thanks

Edit: Adding error log message:
NavMeshAgent is null
UnityEngine.Debug:LogWarning (object)
BehaviorDesigner.Runtime.Tasks.Unity.UnityNavMeshAgent.SetDestination:OnUpdate () (at Assets/Behavior Designer/Runtime/Tasks/Unity/NavMeshAgent/SetDestination.cs:32)
BehaviorDesigner.Runtime.BehaviorManager:RunTask (BehaviorDesigner.Runtime.BehaviorManager/BehaviorTree,int,int,BehaviorDesigner.Runtime.Tasks.TaskStatus)
BehaviorDesigner.Runtime.BehaviorManager:RunParentTask (BehaviorDesigner.Runtime.BehaviorManager/BehaviorTree,int,int&,BehaviorDesigner.Runtime.Tasks.TaskStatus)
BehaviorDesigner.Runtime.BehaviorManager:RunTask (BehaviorDesigner.Runtime.BehaviorManager/BehaviorTree,int,int,BehaviorDesigner.Runtime.Tasks.TaskStatus)
BehaviorDesigner.Runtime.BehaviorManager:RunParentTask (BehaviorDesigner.Runtime.BehaviorManager/BehaviorTree,int,int&,BehaviorDesigner.Runtime.Tasks.TaskStatus)
BehaviorDesigner.Runtime.BehaviorManager:RunTask (BehaviorDesigner.Runtime.BehaviorManager/BehaviorTree,int,int,BehaviorDesigner.Runtime.Tasks.TaskStatus)
BehaviorDesigner.Runtime.BehaviorManager:Tick (BehaviorDesigner.Runtime.BehaviorManager/BehaviorTree)
BehaviorDesigner.Runtime.BehaviorManager:Tick ()
BehaviorDesigner.Runtime.BehaviorManager:Update ()
 
The NavMeshAgent is cached within OnAwake of the Movement Pack tasks. Do you have the NavMeshAgent component attached to the same GameObject as the behavior tree is on? The Movement Pack is doing the same thing that you are doing within your test script:

Code:
            m_NavMeshAgent = GetComponent<NavMeshAgent>();
 
Yes, navmesh agent and behavior are on the same object. Could this be some sort of execution order issue perhaps?
 
Also, when I click on the warning message it takes me to the SetDestination script and to the following line:

Code:
public override void OnStart()
        {
            var currentGameObject = GetDefaultGameObject(targetGameObject.Value);
            if (currentGameObject != prevGameObject) {
                navMeshAgent = currentGameObject.GetComponent<NavMeshAgent>();  <------ HERE
                prevGameObject = currentGameObject;
            }
        }
 
Are you setting a different target? The task will get the navmesh on the target. If you set a breakpoint on that line is currentGameObject what you'd expect?
 
Ok, after using the breakpoint and checking the currentGameObject, I realize that the error is partly my fault. I was putting the destination transform in the Target Game Object slot (because thats what I assumed it was for) but it seems the Target Game Object is for the Object you want to actually move? I'm guessing, which is very counter intuitive as I would think that the object the behavior tree is attached to would be assumed to be the the object you want to move to the destination and the "Target" would be where you want it to move to. I see there is a destination field just below the Target Game Object field in the Set Destination inspector but you cant drag a transform into that slot. In fact Im not really sure what goes in there. It looks like a position/Vector3 but you cant drag a transform into it so I am at a loss as to how to actually populate the destination position...
 
All of the Unity API tasks have a field called "Target" which allows you to specify which GameObject they operate on. If you leave it blank then it will work on the current GameObject. This allows you to retrieve properties or apply methods to other GameObjects.

The Set Destination task uses a SharedVector3. So it's a position value. This is the same input parameter as the SetDestination method: https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent.SetDestination.html

If you are looking for more complex logic I recommend taking a look at the Movement Pack. The Seek task allows you to use a Transform for the destination instead of requiring a Vector3.
 
Ah, ok. I have it sorted out now. I didnt realize it used a shared variable. Now I understand how to supply the destination.

Thanks!
 
Top