Support Forum

Modifying graph at runtime

Hello, I’m using the grid graph for a simple tower defense. I’m generating a map to have walls placed randomly on the grid, when I place a wall I would like to ensure that the wall will not block the path between multiple checkpoints, but it’s not working and I would like to understand what I did wrong.

The code to ensure the wall can be placed:

    public bool CanPlaceObject(Bounds bounds)
        CheckPoint checkPoint = _checkPoints.start;
        GraphUpdateObject guo = new GraphUpdateObject(bounds);
        guo.modifyWalkability = true;
        guo.setWalkability = false;

        List<GraphNode> nodes = new List<GraphNode>();
        while ( != null)
            Vector2Int nodeCoord = GetCoordFromPosition(checkPoint.transform.position);
            var node = _gridGraph.GetNode(nodeCoord.x, nodeCoord.y);
            checkPoint =;
        return GraphUpdateUtilities.UpdateGraphsNoBlock(guo, nodes, true);

This method seems to return true even if the path is blocked.

When a wall is placed, I update the walkability of the node using this method.

    public void SetWalkable(int x, int y, bool walkable)
        _gridGraph.GetNode(x, y).Walkable = walkable;
        _gridGraph.CalculateConnectionsForCellAndNeighbours(x, y);

For example, here is a graph that I generated:
Black dots are checkpoints, red square are unwalkable nodes. As you can see on the screenshot there are no path between both checkpoints.
When I call the method “PathUtilities.IsPathPossible” between the 2 checkpoints, it returns true but when I spawn an agent on the first checkpoint with his destination on the second checkpoint, the agent is stuck with no possible path.

Here is the graph settings:

I hope I gave you enough information to help me out.

Thanks in advance!

Do you run this code inside a work item?
Graphs should not be modified by scripts outside of work items since that is both not safe (pathfinding might be running at the same time) and metadata can get out of date (which is what has happened in this case). Try wrapping it in a work item like this: => {
     // Update the graph here

See Graph Updates during Runtime - A* Pathfinding Project

Yes I tried as well, but I have the same behaviour.
Is the work item executed immediatly or is it processed during the next frames ?

It is executed later, possibly in a later frame.
You can force it to run immediately using

Are you sure the bounding box for the graph update object actually contains the node?

The bounding box was fine, but I found the problem.
The issue is in my own logic, in the method “CanPlaceObject”, the “while” loop is checking != null and it should check for checkpoint != null, the last checkpoint was always skipped.
It was a long time I didn’t use linked list, I’m a little bit rusty about it.

Thanks for your help :slight_smile:

1 Like