How to update graph after instance a game object?

AstarPath.active.UpdateGraphs(bounds);

Cant update graph in Start() and Awake(),
So I update the graph in the next frame after Instance object

public Collider collider;

void Start()
{
    MyUpdateGraph();
}

IEnumerator MyUpdateGraph()
{
    yield return 0;
    Bounds bounds = collider.bounds;
    AstarPath.active.UpdateGraphs(bounds);
}

The above code usually works, but not always
How to update graph after instance a game object?

Hi

Which version are you using?

You might have to call Physics.SyncTransforms (https://docs.unity3d.com/ScriptReference/Physics.SyncTransforms.html) before you try to update the graph in some versions of the package. Otherwise if you created any colliders during the same frame the physics engine might not be up to date. Later versions have fixed this issue.

UpdateGraphs can be called during Start btw, but not during Awake.

1 Like

Unity 2019.3.4f1
A* Pathfinding Project 4.3.17
Solved the problem using your method , thank you

Huh. If you are using 4.3.17 then the package should already run SyncTransforms before it does an update. Are you sure this was the solution?

Yes, I just tried

private void Start()
{
    Bounds bounds = collider.bounds;
    AstarPath.active.UpdateGraphs(bounds);
}

Use above code , every Obstacle dont have correct collider , agent can through them.

private void Start()
{
    Physics.SyncTransforms();
    Bounds bounds = collider.bounds;
    AstarPath.active.UpdateGraphs(bounds);
}

Use above code , every Obstacle have correct collider , agent cant through them.

private void Start()
{
   StartCoroutine(MyUpdateGraph());
}
IEnumerator MyUpdateGraph()
{
    yield return 0;
    Bounds bounds = collider.bounds;
    AstarPath.active.UpdateGraphs(bounds);
}

Use above code , 5% - 25% Obstacle don’t have correct collider.

That is very strange. Would it be possible for you to share a small example project that shows this issue?