Everything works well up until the point we reintroduce the ExploreNeighbours(). After I started the operation queue.Enqueue(neighbour); Unity will freeze and I have to force a close from TaskManager. If queue.Enqueue(neighbour) is commented out, the crash never occurs. Also, the crash doesn’t occur if endWaypoint is right next to StartWaypoint. This is on version 2019.3.8f1. Any help please as to what the issue could be?
Pathfinder.cs hereunder:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pathfinder : MonoBehaviour
{
[SerializeField] Waypoint startWaypoint;
[SerializeField] Waypoint endWaypoint;
Dictionary<Vector2Int, Waypoint> grid = new Dictionary<Vector2Int, Waypoint>();
Queue<Waypoint> queue = new Queue<Waypoint>();
[SerializeField] bool isRunning = true;
Vector2Int[] directions = {
Vector2Int.up,
Vector2Int.right,
Vector2Int.down,
Vector2Int.left
};
void Start()
{
LoadBlocks();
ColourStartAndEnd();
Pathfind();
}
private void Pathfind()
{
queue.Enqueue(startWaypoint);
while (queue.Count > 0 && isRunning)
{
var searchCentre = queue.Dequeue();
print("Searching from " + searchCentre);
HaltIfEndFound(searchCentre);
ExploreNeighbours(searchCentre);
}
print("finished pathfinding?");
}
private void HaltIfEndFound(Waypoint searchCentre)
{
if (searchCentre == endWaypoint)
{
print("EndWaypoint reached");
isRunning = false;
}
}
private void ExploreNeighbours(Waypoint from)
{
if (!isRunning) { return; }
foreach (Vector2Int direction in directions)
{
Vector2Int neighbourCoordinates = startWaypoint.GetGridPos() + direction;
try
{
Waypoint neighbour = grid[neighbourCoordinates];
neighbour.SetTopColour(Color.yellow);
queue.Enqueue(neighbour);
}
catch
{
}
}
}
private void LoadBlocks()
{
var waypoints = FindObjectsOfType<Waypoint>();
foreach(Waypoint waypoint in waypoints)
{
var gridPos = waypoint.GetGridPos();
if (grid.ContainsKey(gridPos))
{
Debug.LogWarning("Overlapping block found " + waypoint);
}
else
{
grid.Add(gridPos, waypoint);
waypoint.SetTopColour(Color.black);
}
}
}
private void ColourStartAndEnd()
{
startWaypoint.SetTopColour(Color.blue);
endWaypoint.SetTopColour(Color.red);
}
}