Error when updating graph loaded from cache

I’m trying to implement an optimized export feature in unity for my game, and part of it is caching the graph. This is the code I use:

        AstarPath.MenuScan();
        AstarPath.active.astarData.SaveCacheData(Pathfinding.Serialize.SerializeSettings.All);

After this when I run the game I get the following error message when I’m trying to update the graph using AstarPath.active.UpdateGraphs(collider.bounds):

NullReferenceException: Object reference not set to an instance of an object
Pathfinding.GridGraph.CalculateConnections (Pathfinding.Node[] nodes, Int32 x, Int32 z, Pathfinding.Nodes.GridNode node) (at Assets/AstarPathfindingProject/Generators/GridGenerator.cs:778)
Pathfinding.GridGraph.UpdateArea (Pathfinding.GraphUpdateObject o) (at Assets/AstarPathfindingProject/Generators/GridGenerator.cs:1139)
AstarPath.DoUpdateGraphs () (at Assets/AstarPathfindingProject/Core/AstarPath.cs:934)
AstarPath.RegisterSafeUpdate (.OnVoidDelegate callback, Boolean threadSafe) (at Assets/AstarPathfindingProject/Core/AstarPath.cs:1685)
AstarPath.QueueGraphUpdates () (at Assets/AstarPathfindingProject/Core/AstarPath.cs:769)
AstarPath.UpdateGraphs (Pathfinding.GraphUpdateObject ob) (at Assets/AstarPathfindingProject/Core/AstarPath.cs:876)
AstarPath.UpdateGraphs (Bounds bounds) (at Assets/AstarPathfindingProject/Core/AstarPath.cs:840)
UpdateGraphWhenDestroyed.OnDeath () (at Assets/NTD/Scripts/OnDeath/UpdateGraphWhenDestroyed.cs:11)
UnityEngine.Component:BroadcastMessage(String, SendMessageOptions)
SimpleHPHolder:HandleHit(Damager) (at Assets/NTD/Scripts/DestroyOnCondition/SimpleHPHolder.cs:15)
UnityEngine.Component:BroadcastMessage(String, Object, SendMessageOptions)
Hitable:GotHit(Damager) (at Assets/NTD/Scripts/Hitable.cs:176)
Orc:Update() (at Assets/NTD/Scripts/Orc.cs:20)

Furthermore when I stop the game I get this error:

Could not terminate pathfinding thread[0] in 50ms, trying Thread.Abort
UnityEngine.Debug:LogError(Object)
AstarPath:OnDestroy() (at Assets/AstarPathfindingProject/Core/AstarPath.cs:1241)

I’ve never seen these messages before switching to cached loading.
Thanks for the help.

Hi

That is a known bug with load from cache + grid graph.
See http://www.arongranberg.com/forums/topic/problem-updating-loaded-grid-graph/#post-2255 for a workaround.

Cheers,
Aron

Thanks for the quick reply, I really appreciate it. I’ll see if it fixes my problem.
Since I started my discussion here I have another related issue: sometimes graph caching is not enabled and the cached graph is not saved using the 2 lines of code above.

Hi

How do you mean? Does the graph not get saved when graph caching is off?
What exactly are you trying to accomplish?
Also, this is in the Unity Editor, right? The above code will not work in a built player.

Yes, I’m using it in the editor. What I’m trying to acoomplish is turning on graph caching and saving the cached graph into the A* object in a scene.

It works most of the time, but sometimes I’ll see that caching remained turned off and so it is not saved.

Hi

Cache is always enabled when calling that function. Make sure that you call SetDirty on the AstarPath object when you do any changes, otherwise Unity might not serialize it.

Also, you know that graphs can be exported to a file as well, right? Thought it might happen to be better in your case.

Thanks for the new replies. SetDirty seems to have fixed the issue, although I didn’t have a lot of time to test it.
I know about the save to file feature, but I really wanted to create a self-contained scene.