- Home /
Unity crashes when doing A* pathfinding loop
I have a A* pathfinding class that I implemented myself with psuedocode as help. The program crashes when it is suppose to do a do-while loop that runs unitl target is found or the "OpenList" is empty. The problem seems to be with the OpenList, but I can't figure out what is wrong.
The list is an ArrayList and the loop runs while openList.Count is not 0. I did try to change the condition to just while pathFound == false, but it still crash. What happens when it crash is I can't do anything with Unity and windows labels it as "Not responding".
My own speculation is that it somehow becomes an infinite loop, but I've checked and double checked all functions and they work the way I want. It works when I remove the do-while loop and all values are what I expect them to be.
Well, this is my code. Can anyone find something that might cause the problem?
do{
closedList.Add(current); //Switch to closed list
openList.Remove(current);
neighbors = GetNeighbors(current);
foreach (Node neighbor in neighbors){
if ((tileMap[neighbor.xpos, neighbor.zpos].Walkable()) && (!ClosedListContain(neighbor))){
if (!OpenListContains(neighbor)){///(!openList.Contains(neighbor)){ //
Debug.Log("Neighbor addede to openList");
openList.Add(neighbor); //openList now contains this node
neighbor.SetParent(current);
SetScores(neighbor);
}
else{
Debug.Log("No neighbor added");
Node neighborClone = neighbor;
SetScores(neighborClone);
if (neighborClone.G < neighbor.G){ //Current path to this node is quicker
neighbor.SetParent(current);
SetScores(neighbor);
}
}
}
}
if (ClosedListContain(goal)){
pathFound = true;
break;
}
} while(openList.Count != 0);
Your telling it to do whats in that loop while your open list is not 0. In your do loop you are adding to that list. So it never will stop.
Answer by SnStarr · Feb 03, 2015 at 07:41 AM
} while(openList.Count != 0);
It will always be a number greater than 0 as you are adding something to list in your do loop. You will be forever stuck in that while loop because there will always be an item in the openList.count. Unity will not process endless loops. Break the loop or give it another statement.
Yes I see the error now, thank you. The first thing to be done each loop is to get a new current, like this:
current = GetNodeWithLowestF();
This way one value of the openList will be removed each loop. I can run the loop without crashing now. I even had that function written, I have no idea why it wasn't used. Stupid simple mistakes. Thank you and SnotE101 for pointing it out.
No problem, and good luck on future endeavors. Remember, logic always matters.
Your answer
Follow this Question
Related Questions
Loop crashing unity (pathfinding) 2 Answers
Unity crashes because of script 2 Answers
Logic question, unique path finding system 2 Answers
Pathfinding function crashes Unity? 1 Answer
Loop to populate array crashes Unity 1 Answer