A* Pathfinding Project

GridGraph nodes array empty


#1

I changed my terrain from Unity Terrain to Mesh GameObject and now GridGraph.nodes returns empty after setting up a GridGraph at runtime as usual (adjusting position and dimensions to Mesh GameObject). GridGraph.nodes.Length throws a NullReferenceException error. This single, properly scanned, GridGraph is visible in the scene along with the terrain mesh, pathfinding seems to be carried out normally and GridGraph.width returns values as usual. Does anyone have any idea what may cause this?

Thanks!


#2

Hi

Are you sure you haven’t added multiple grid graphs by any chance?
What is your code for setting this up?
Do you get any other errors in the console?


#3

Hi Aron,

I just ran this in a new project with the same result.

Hierarchy:

  • Camera
  • Light
  • A* Pathfinding GameObject
  • Empty GameObject with script

Project:

  • “Resources” directory containing “Plane” prefab, holding an unchanged plane primitive with black material.

Script:
"
using UnityEngine;
using Pathfinding;

public class CreateTerrain : MonoBehaviour {
int width = 128;
int length = 128;
float nodeSize = 1;
GameObject terrain;
Vector3 terrainPosition;
Vector3 terrainSize;
GridGraph graph;
AstarData data;

void Start () {
    terrainPosition = new Vector3(0, 0, 0);
    terrainSize = new Vector3(width, 100, length);
    int ggwidth = width * 10;
    int gglength = length * 10;

    // INSTANTIATE TERRAIN
    terrain = Instantiate(Resources.Load<GameObject>("Plane"), terrainPosition, Quaternion.identity);
    terrain.gameObject.transform.localScale = terrainSize;

    // ADD GRIDGRAPH
    data = AstarPath.active.data;
    graph = data.AddGraph(typeof(GridGraph)) as GridGraph;

    // SET UP GRIDGRAPH
    graph.center = terrainPosition;
    graph.SetDimensions(ggwidth, gglength, nodeSize);

    // PRINT GG VALUES
    print(graph.width);
    print(graph.nodes.Length);
}

}
"

Thanks!


#4

Problem solved! I called Active.data.gridGraph.Scan() only after trying the access the nodes array in my project and left it out in my previous post completely. Calling gridGraph.Scan() populates the nodes array.