Rescale Grid Graph during run-time

Hello,

I am trying to rescale a grid graph based off of user input at runtime. I have read answers to similar questions but have not been able to successfully update the graph. I am currently using this code:

`  GameObject A = GameObject.Find ("A*");
AstarPath astar = A.GetComponent("AstarPath") as AstarPath;
astar.Awake();

Pathfinding.AstarData data = AstarPath.active.astarData;

Pathfinding.GridGraph gg = data.graphs [0] as Pathfinding.GridGraph;
//Pathfinding.GridGraph gg = data.AddGraph(typeof(GridGraph)) as GridGraph;

gg.width = maxCols*mazewidth*2;
gg.depth = maxRows*mazewidth*2;
gg.nodeSize = 0.5f;

gg.UpdateSizeFromWidthDepth();

AstarPath.active.Scan();`

I am accessing the first graph instead of creating a new one because I already have a gameobject with the Astar Path script on it which has a grid graph already created as well. Also, this code is called in the Awake() function.

I confirmed through debug statements that the graph was being updated through the UpdateSizeFromWidthDepth call, but that change was not reflected in the inspector or game gizmos.

The other answers seemed to say that what I have is the solution but I can’t seem to recreate that. Any help would be appreciated.

Thank you

Hi

It might be that the AstarPath.Awake method is called after your Awake method, if it did that, it would overwrite your changes.

Try instead to either

  1. Not call the code in Awake or
  2. Remove the astar.Awake line and instead adjust the unity Script Execution Order and make sure AstarPath is executed before your script.

Also to avoid scanning the graph twice, you can uncheck A* Inspector -> Settings -> Scan On Awake.

Hello,

That was exactly what the problem was.

Thanks for the quick reply.