Raycast + RichAI Teleport Error on Init()

Getting the following error with a basic test where I have RichAI attached to a character. On a flat plane with target set to empty gameobject.

NullReferenceException: Object reference not set to an instance of an object
Pathfinding.RichAI.Teleport (Vector3 newPosition) (at Assets/AstarPathfinding/Core/AI/RichAI.cs:254)
Pathfinding.RichAI.Init () (at Assets/AstarPathfinding/Core/AI/RichAI.cs:233)
Pathfinding.RichAI.OnEnable () (at Assets/AstarPathfinding/Core/AI/RichAI.cs:225)

The reason it falls into this code, is because of this check inside init():

if (prevPosition != tr.position) Teleport(tr.position);

Why is it checking for prevPostion onEnable()? This seems to be triggering a Teleport on awake, which causes an error. Not seeing an example that sets prevPosition either.

To clarify, the problem seems to be happening whenever I try to set the target through code during runtime, rather than link it through the handler in editor. I tried assigning target to RichAI in awake and start without any luck. All the examples seem to already have a Target on scene before it starts.

Hi

Which version are you using?
Which exact line of code is throwing the exception?

Hello,

Version: 4.0.10
Line of code: var nearest = AstarPath.active.GetNearest(newPosition); (RichAi.cs line 254)
Assets used for repro: Astar Pathfinder Pro, Character Standard Assets

Repro steps

  1. Add cube as floor.
  2. Add Ethan model and target onto floor.
  3. Add astar, recast graph, snap to bounds and scan.
  4. Add RichAI to Ethan model, and set target.
  5. Press play and see error in console:

NullReferenceException: Object reference not set to an instance of an object
Pathfinding.RichAI.Teleport (Vector3 newPosition) (at Assets/AstarPathfindingProject/Core/AI/RichAI.cs:254)
Pathfinding.RichAI.Init () (at Assets/AstarPathfindingProject/Core/AI/RichAI.cs:233)
Pathfinding.RichAI.OnEnable () (at Assets/AstarPathfindingProject/Core/AI/RichAI.cs:225)

If I use AIPath instead, it works fine without any errors.

Ah.
I think this is due to the execution order of the scripts which is somewhat undefined in Unity.
It works in my project, but I didn’t think about it enough it seems. Try to open the Execution Order settings in Unity, add the RichAI script and make sure it has a positive execution order (e.g 100).

That this is even called is to move the agent to the closest point on the navmesh.
I will see if I can make it work some other way that doesn’t require a different execution order.

1 Like

I was just having the same issue and resolved it in a slightly different way. I added AstarPath to the Script Execution Order and assigned it a value of -100 (that is, after all the other Pathfinding scripts, but before Default Time). Would this break anything?

For those who don’t know where this setting is in Unity, it is:

Edit -> Project Settings -> Script Execution Order.

1 Like