Setting up nodes for randomly generated maze

Hello UnityAnswers.

I’ve got a randomly generated maze that I need to have an AI traverse. I know to use A* but I’m having trouble setting up the waypoints for it to search with.

Essentially, I need each waypoint to get and hold the waypoint immediately to it’s north, south, east, and west. The waypoints cannot have rigidbodys or active colliders on them.

I tried sphere casting, but that required the waypoints to have colliders on them.

One way I thought of doing it is, for each waypoint, get an array of all GameObjects with the tag “waypoint,” then search that array for the adjacent one. For instance, to get the north waypoint:

GameObject[] waypoints = GameObject.FindObjectsWithTag("waypoint");
for (int i = 0; i < waypoints.Length; i++){
   if (waypoints*.transform.position.x == transform.position.x &&*

waypoints*.transform.position.z == transform.position.z + 3){*
north = waypoints*;*
i = waypoints.Length;
//Note: every waypoint is 3 units away from their neighbors
}
}
This is very inefficient as it will run for every waypoint. Anyone got a better way?

Try this instead:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class Test : MonoBehaviour {
    
    
        public GameObject waypointParent;
    
        public List<Transform> waypointNodes;
    
    	// Use this for initialization
    	void Start () 
        {
            waypointParent = GameObject.FindGameObjectWithTag("waypoint"); //Parent gameObject of the waypoint nodes.
    
            foreach(Transform go in waypointParent.GetComponentsInChildren<Transform>())
            {
                if(go.position.x == this.transform.position.x && go.position.z == (this.transform.position.z + 3))
                {
                    waypointNodes.Add(go);
                }
            }
    	}
    
    }