Problem with loop the get better path to goal

Hey everyone, I’m slowly trying to acquire some results with path finding and started reading some articles of A* to get a base. I don’t know if I’m in the right path but I’ve reached this. (if you guys want the whole code I can post it here, i just didn’t because I coded it in portuguese and take me a lot to translate).

The problem in the function below is that it’s breaking Unity. Probably because of the loop.

What ‘hasClearPath’ does is basically launch a Linecast from the position of the last node of the list to the goal.

public void findBestPath() {
    		
    		closedList.Clear();
    		
    		betterPath.Clear();
    	
    		openList = Nodes;
    		
    		betterPath.Add( findFirstNode() );
    		
    		while(true) {
    			
    			List<Nodes> Neighbors = findNeighbors( betterPath[betterPath.Count - 1] ); // returns a list of neighbors of the last item in betterPath
    			
    			betterPath.Add( findBetterNeighbor( Neighbors ) ); // find the better neighbor( lowest F) between Neighbors returned above and add him to the list
    			
    			if(betterPath[betterPath.Count - 1].hasClearPathTo(Goal)) { // if the last item of betterPath has clear path to the goal, breaks the loop
    				
    				break;
    				
    			}
    			
    		}
    		
    		
    	}

Any hint on this?

Thanks from now!

Well, the while true is not going to do any good there. It makes sense when you use it in a coroutine which is paused every frame or x seconds, but there your code juste get stucked inside the loop. So yield null or find a way to leave the loop at some point.