Cannot seem to scan graph from script

Hi,
I’m procedurally generating a 2D tilemap and want to scan the graph after having done that.
However, it doesn’t quite appear to work.
I’m using AstarPath.active.Scan() after having generated the tilemap, however, it shows the entire map as blue. If I then simply hit the scan button in the unity editor, it does what I expected.

Should I not be able to simply scan from script?
My grid graph has been set up in the editor, and I’m I just set the size and ask it to scan, in my script.

Code:

void GeneratePathGraph(int width, int height)
    {
        // get graph
        var gg = AstarPath.active.data.gridGraph;

        float nodeSize = 0.5f;
        gg.center = new Vector3(width / 2f, height / 2f, 0);

        // Updates internal size from the above values
        gg.SetDimensions(width * 2, height * 2, nodeSize);

        // Scans graph
        AstarPath.active.Scan(gg);
    }

Hi

If you are creating a bunch of colliders it is possible you might have to wait for a frame or explicitly tell the physics system to sync everything. Otherwise when the graphs are being scanned it may not pick up the new colliders.

See also https://docs.unity3d.com/ScriptReference/Physics.SyncTransforms.html

1 Like

That fixed it, thanks!

I’ve put a WaitForEndOfFrame() in before scanning, and it all seems to work now.
Is there a smarter way, to detect that colliders are ready, other than waiting a fixed time? Any events maybe?

1 Like

Hi

You could try to call Physics.SyncTransforms (linked above). Right before you scan. That might be enough.

Couldn’t quite make a SyncTransorms call work. Oh well, waitForEndOfFrame seems to do the trick.

Thanks again.

1 Like