Unity freezing when I start adding to Queue

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);
    }
}

Hi Gansher,

When a program freezes, it is very likely that there is an endless loop somewhere in the code. Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

Hi Nina,

Thank you for your help. The issue is in the ExploreNeighbours method in my case. When I declare the neighbourCoordinates variable, I am using the startWaypoint instead of the from Waypoint being passed in the method.

Don’t exactly understand how this is causing the infinite loop, however it’s solved now. Thanks so much! :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms