Unable to getNearest Node (Grid Graph)

Hey Guys
I’m having a lot of trouble finding the nearest node in my current project.
I have an array of transforms called All checkpoints that I set in the editor. Then At startup I run the following method.

    private void GetPathNodes()
    {
       // Transforms actually defined higher in the script and set in editor.
        public Transform[] allCheckpoints; 
        List<GraphNode> allPathNodes;
        foreach(Transform tf in allCheckpoints)
        {
            Debug.Log("Checkpoint = " + tf.gameObject.name);
            GraphNode thisNode = AstarData.active.GetNearest(tf.position).node;
            //GraphNode thisNode = pathfinder.GetNearest(tf.GetChild(0).position).node;
            Debug.Log("Node Object Loc= " + thisNode.position);
            allPathNodes.Add(thisNode);
        }
    }

However This returns a null node 100% of the time, even when the transform is directly on top of or inside the grid graph. I can’t seem to figure out why its unable to locate a node.
Can anyone offer some guidance?

The logs appear as follows:

Checkpoint = tempStart
UnityEngine.Debug:Log(Object)
GemPlacement:GetPathNodes() (at Assets/Scipts/GemPlacement.cs:43)
GemPlacement:Start() (at Assets/Scipts/GemPlacement.cs:36)

Node Object Loc= ( -38500, 0, -37500)
UnityEngine.Debug:Log(Object)
GemPlacement:GetPathNodes() (at Assets/Scipts/GemPlacement.cs:46)
GemPlacement:Start() (at Assets/Scipts/GemPlacement.cs:36)

NullReferenceException: Object reference not set to an instance of an object
GemPlacement.GetPathNodes () (at Assets/Scipts/GemPlacement.cs:47)
GemPlacement.Start () (at Assets/Scipts/GemPlacement.cs:36)

Hi

You are not initializing your allPathNodes to anything, so it will be null when you try to add a node to it.

1 Like

Thanks Aron! I’ll try that but I think you’re absolutely right!
What a rookie mistake!

1 Like