Calling RemoveGraph while graph is being scanned

  • A* version: 5.1.6
  • Unity version: 6000.1.37

Maybe similar to:

I’m experiencing the following error:

“InvalidOperationException: Graphs cannot be added, removed or serialized while the graph structure is locked. This is the case when a graph is currently being scanned and when executing graph updates and work items.
However as a special case, graphs can be added inside work items.”

When I try to RemoveGraph during a OnDestroy of a game object. It is a similar use case to the one mentioned fro Badjano in the separate thread, only the graph isn’t getting destroyed during a scene unload, but instead, the graph is being destroyed when I destroy a game object.

OnDestroy()
{
AstarData data = AstarPath.active.data;
if ( m_OwnerOfGridGraphInstance )
{
data.RemoveGraph ( m_GridGraph );
}
}

Would wrapping the data.RemoveGraph in a AddWorkItem/AstarWorkItem request solve the issue?

Please do take any action in replying as I seem to have fixed the issue using AstarWorkItem and ensuring that

    GridGraph.gizmoRenderTransformToLocal  and GridGraph.gizmoRenderTransform 

of the graph being removed are set to null.

    void OnDestroy( )
    {
      if ( m_GridGraph != null )
      {
        m_GridGraph.gizmoRenderTransformToLocal = null;
        m_GridGraph.gizmoRenderTransform = null;
      }

      if ( AstarPath.active != null )
      {
        AstarPath.active.AddWorkItem ( new AstarWorkItem ( ctx =>
        {
          AstarData data = AstarPath.active.data;
          data.RemoveGraph ( m_GridGraph );
        } ));
      }
}

I didn’t see your reply until after I had already spent time testing this :joy: For what it’s worth, I got it to delete without using a work item, but that’s probably down to the simplistic nature of my testing environment.

Glad you sorted it out though :+1:

Thanks for checking and sorry for the wild goose chase.

1 Like