Editing GridGraph at Runtime

Hi there,

I have set up a gridgraph with the following settings:

I Instantiate a cube prefab and assign a random position to it every 1 second, and consider it an unwalkable part of the graph.

private void Refresh()
    {
        _obj.SetActive(false);
        var bounds = _obj.GetComponent<Collider>().bounds;
        var guo = new GraphUpdateObject(bounds);
        guo.updatePhysics = true;
        AstarPath.active.UpdateGraphs(guo);
        
        _obj.SetActive(true);
        _obj.transform.position = new Vector3(Random.Range(0, 10f), 0f, -Random.Range(0, 10f));
        bounds = _obj.GetComponent<Collider>().bounds;
        guo = new GraphUpdateObject(bounds);
        guo.updatePhysics = true;
        AstarPath.active.UpdateGraphs(guo);
    }

I expect the first part of the code to remove the previous position of the cube on the graph, and the second part, add a unwalkable hole on the graph.
Unfortunately the result I get is multiple holes that represent the change done during the previous refresh… No holes get cleared up.

Is this process not synchronous? is it due to multithreading?

Could it be related to that thread? Cannot seem to scan graph from script
Unfortunately calling Physics.SyncTransforms
cause a weirder issue and never seem to update the graph at all.

It seems to be related to that issue:

I found three options to fix this issue:

1- Wait for the end of the frame (I do not like this solution as I need to refresh my grid synchronously)
2- Set Physics.autoSyncTransforms = true; (Physics.SyncTransforms doesn’t seem to be enough)
3- Refresh collider by calling
collider.enabled = false;
collider.enabled = true;

Hi

UpdateGraphs does indeed not actually update the graph. It just puts it in a queue and the graph will be updated later. This is done for performance.

You can force the graph to be updated immediately by calling AstarPath.active.FlushGraphUpdates();

See https://arongranberg.com/astar/docs/astarpath.html#FlushGraphUpdates