Hello, I am trying to implement some very basic pathfinding. Here is the main excerpt from my code; Code (csharp): using UnityEngine; using System.Collections; using System.Collections.Generic; //Tile class public class Tile { public TileState tileState; //The state of the tile - empty, wall, floor etc public int x; //x position (Normalized) public int y; //y position (Normalized) public int g; //Cost public int h; //Manhattan Distance public int f; //Total cost public Tile parent_tile; } public class TileValues { public int empty = 0; public int wall_persp = 0; public int floor = 1; public int edge = 1; } public class pathfinding_test : MonoBehaviour { //A* corridor pathfinding void DigCorridors() { Debug.Log("Starting DigCorridors()..."); Debug.Log("Calling build_path()..."); List<Tile> corridor = build_path(grid_list[5], grid_list[15]); Debug.Log("Called build_path()."); for(int i = 0; i < corridor.Count; i++) { grid_list[grid_list.IndexOf(corridor[i])].tileState = TileState.Floor; } Debug.Log("Calling Fill()..."); Fill(); } //City block distance - used in pathfinding int manhattanDistance(Tile from, Tile to) { return Mathf.Abs(from.x - to.x) + Mathf.Abs(from.y - to.y); } List<Tile> DigPath(Tile from, Tile to) { Debug.Log("Starting DigPath()..."); //Define the lists of tiles we can reach, and tiles we have explored List<Tile> open = new List<Tile>(); List<Tile> closed = new List<Tile>(); //Add the from tile to the reachable list open.Add(from); Debug.Log("Starting A* loop at: " + Time.timeSinceLevelLoad ); while(open.Count > 0) { Debug.Log("while loop"); Tile node_current = new Tile(); Debug.Log("Getting lowest F score"); node_current = GetLowestFScore(open, to); Debug.Log("Got lowest F score"); if(node_current == to) { break; } Debug.Log("Getting adjacent nodes"); List<Tile> adjacent_tiles = new List<Tile>(); adjacent_tiles = get_adjacent_nodes(node_current); Debug.Log("For loop"); for(int i = 0; i < adjacent_tiles.Count; i++) { if(adjacent_tiles[i] == to) { break; } Tile adjacent_node = new Tile(); adjacent_node = adjacent_tiles[i]; adjacent_node.g = node_current.g + 1; adjacent_node.h = manhattanDistance(adjacent_node, to); adjacent_node.f = adjacent_node.g + adjacent_node.h; if(open.Contains(adjacent_node) && node_current.f < adjacent_node.f) { //Discard and continue continue; } if(closed.Contains(adjacent_node) && node_current.f < adjacent_node.f) { //Discard and continue continue; } adjacent_node.parent_tile = node_current; open.Add(adjacent_node); } closed.Add(node_current); } Debug.Log("Finished A* loop at: " + Time.timeSinceLevelLoad ); return closed; Debug.Log("Finished DigPath()."); } Tile GetLowestFScore(List<Tile> open_list, Tile to) { Debug.Log("Starting GetLowestFScore()..."); int lowest_f = 1000000; Tile lowest_f_tile = new Tile(); Debug.Log("GetLowestFScore() if statement"); if(open_list.Count == 1) { lowest_f_tile = open_list[0]; Debug.Log("Got lowest f"); } else { Debug.Log("GetLowestFScore() for loop"); for(int i = 0; i < open_list.Count; i++) { if(open_list[i].f < lowest_f) { lowest_f_tile = open_list[i]; lowest_f = open_list[i].f; } } } Debug.Log("Returning lowest F tile"); return lowest_f_tile; } List<Tile> get_adjacent_nodes(Tile node) { List<Tile> adjacent = new List<Tile>(); //Add N tile if(grid_list.IndexOf(node) - gridSettings.Width > 0) { adjacent.Add(grid_list[grid_list.IndexOf(node) - gridSettings.Width]); } //Add S tile if(grid_list.IndexOf(node) + gridSettings.Width < grid_list.Count) { adjacent.Add(grid_list[grid_list.IndexOf(node) + gridSettings.Width]); } //Add E tile if(grid_list.IndexOf(node) + 1 < grid_list.Count) { adjacent.Add(grid_list[grid_list.IndexOf(node) + 1]); } //Add W tile if(grid_list.IndexOf(node) - 1 > 0) { adjacent.Add(grid_list[grid_list.IndexOf(node) - 1]); } return adjacent; } List<Tile> build_path(Tile from, Tile to) { Debug.Log("Starting build_path()..."); List<Tile> path = new List<Tile>(); path = DigPath(from, to); while(to != null) { path.Add(to); to = to.parent_tile; } return path; Debug.Log("Finished build_path()."); } } Currently, when I run this, unity crashes. What am I doing wrong?
Forgot to say, I should be able to run build_path(), which should return a List of 'tiles' (that list is the path)
If unity is crashing on you, try looking at the error logs. It might tell you what line caused the issue. http://docs.unity3d.com/Manual/LogFiles.html
I tried this, but it doesn't seem to be logging anything before the editor freezes. I do have to relaunch unity though, which is probably why.