Help with pathfinding function!

Hey everyone. Can someone help me poiting some errors in this Pathfinding function?

This is just breaking Unity.

void AStar (Node initialNode, Node finalNode) {
			openList = closedList = new List<Node>();
			initialNode.G = 0;
			initialNode.H = initialNode.GetH( finalNode );
			Node current = null;
			while(openList.Count != 0) {
				current = openList[0];
				if(current == finalNode) {
				foreach(Node neighbor in current.Neighbors) {
					if(closedList.Contains(neighbor)) {
					} else {
					if(!openList.Contains(neighbor)) {
						neighbor.GetF(current, finalNode);
						} else {
							int neighborG = neighbor.GetG(current);
							if(neighborG < neighbor.G) {
								neighbor.G = neighborG;
					openList.Sort(delegate(Node x, Node y) {
						return x.F.CompareTo(y.F);

And here’s the Node class :

public class Node {

	public Vector3 position;
	public List<Node> Neighbors; // these are defined with a function that I omitted, but I'm sure that it doesn't return a empty list
	public Node parentNode;
	public int F, G, H;
	public Node (Vector3 Position, bool DefineNeighbors) {
		position = Position;
	public int GetF (Node currentNode, Node finalNode) {
		this.G = GetG(currentNode);
		this.H = GetH(finalNode);
		this.F = this.G + this.H;
		return this.F;
	public int GetH (Node finalNode) {
		H = (int)(Mathf.Abs(this.position.x - finalNode.position.x) + Mathf.Abs(this.position.z + finalNode.position.z));
		return H;
	public int GetG (Node currentNode) {
		int distanceToCurrent = (int)(Mathf.Abs(this.position.x - currentNode.position.x) + Mathf.Abs(this.position.z + currentNode.position.z));
		G = currentNode.G + distanceToCurrent;
		return G;
	public void defineParentNode(Node parent) {
		this.parentNode = parent;


If someone have the time to read the code and point something that could be make me get stuck in the loop, I’ll appreciate!

Thanks from now!

You’re never removing nodes from your open list.
I think it should be

current = openList[0];

Going through your code, I’ll agree with Alex. You’re never removing a node from the list, because of this, your count in the list is never reaching 0, therefore, never ending. Unity will always freeze/crash if an infinite loop happens, and in this case, it never once will leave the while loop, also shown by the example code you gave me.

Upon looking at the code you’re using, assuming [this][1], you can see that the method current == finalNode, may be true, and may run that reconstructPath, but this does nothing to openList, which is ultimately the problem.

Some work arounds, that I don’t know if it’ll work, but may strike an idea from you:

  1. Make multi-[thread][2], so this works along with the main [thread][3] (Don’t know if Unity does this, I assume it’s possible though).
  2. Have another list, something similiar to backedUpOpenList, and put all the openList in it, then remove the header and do the while it’s not null, then return so said list when done with w/e you need to be done with.
  3. Write your own A* program ;). You’ll have more control, but no seriously, I think option 2 will work.

Hope this helps a bit, but yes, ultimately I believe Alex was right. =). Good luck!
[2]: About multiple Thread - Questions & Answers - Unity Discussions
[3]: unity3d threads - Google Search