Script is reading cubes but not in the order that I am expecting

The script is reading cubes not in the same order that I am expecting. It seems that it starts clockwise from right instead of from Top (up)

This is the part of code that is related to this section:

public class PathFinder : MonoBehaviour
{
    [SerializeField] Waypoint startWaypoint, endWaypoint;
    
    Dictionary<Vector2Int, Waypoint> grid = new Dictionary<Vector2Int, Waypoint>();
    Queue<Waypoint> queue = new Queue<Waypoint>();
    [SerializeField]bool isRunning = true;  //TODO make private

    Vector2Int[] directions =
    {
        Vector2Int.up,Vector2Int.right, Vector2Int.down,Vector2Int.left
    };

    void Start ()
    {
        LoadBlocks();
        ColorStartAndEnd();
        FindPath();
	}

    private void FindPath()
    {
        queue.Enqueue(startWaypoint);
        

        while (queue.Count>0 && isRunning)
        {
            var searchCenter = queue.Dequeue();
            searchCenter.isExplored = true; 

            print("Searching from " + searchCenter); //TODO remove log 
            HaltIfEndFound(searchCenter);
            ExploreNeighbors(searchCenter);

        }

        print("finish finding path?"); 
    }

    private void HaltIfEndFound(Waypoint searchCenter)
    {
        if (searchCenter == endWaypoint)
        {
            print("search end node, then stopping!"); //TODO remove log 
            isRunning = false; 
        }
        
    }

    private void ColorStartAndEnd()
    {
        startWaypoint.SetTopColor(Color.green);
        endWaypoint.SetTopColor(Color.red); 
    }

    private void LoadBlocks()
    {
        var waypoints = FindObjectsOfType<Waypoint>();

        foreach (Waypoint waypoint in waypoints)
        {
            var gridPos = waypoint.GetGridPos();
            //bool isOverLaping = grid.ContainsKey(gridPos);
            if (grid.ContainsKey(gridPos))
            {
             //   Debug.LogWarning("Skipping overlapping block" + waypoint);
            }
            else
            {
                grid.Add(gridPos, waypoint);
                
                waypoint.SetTopColor(Color.black);
            }
            
        }
        print("Loaded " + grid.Count + " blocks");
    }
    
    private void ExploreNeighbors(Waypoint from)
    {
        if (!isRunning) { return;  }
        foreach (Vector2Int direction in directions)
        {
            Vector2Int ExplCoord = from.GetGridPos() + direction;
           // print("Exploring" + ExplCoord);
            try
            {
                QueueNewNeighbors(ExplCoord);
            }
            catch
            {
               // Debug.LogError("there is no cube here");
            }

        }
    }

    private void QueueNewNeighbors(Vector2Int ExplCoord)
    {
        Waypoint neighbor = grid[ExplCoord];
        if (neighbor.isExplored || queue.Contains(neighbor))
        {

        }
        else
        {
            neighbor.SetTopColor(Color.blue); 
            queue.Enqueue(neighbor);
            print("Queuing "+neighbor);
        }

    }


}

foreach is “supposed” to iterate over a collection in order, but I’ve demonstrated to my own satisfaction that it cannot be counted to always iterate from the correct starting point…
Try this:

 for (int i=0;i<directions.Length;i++)
 {
      Vector2Int direction = directions[i];
      ...
 }

Wow !
Thanks

Privacy & Terms