While loop won't work, it freezes Unity

So I’m trying to use a while loop to have my program find a path through an 8x8 array. However on launch, Unity stops responding and has to be closed. Can someone help explain why my code doesn’t work as I don’t see anything wrong with it as the condition for it to end is changed.
Here is the source code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LevelGenerator : MonoBehaviour {
	
	public int counter = 0;
	
	int[,] LevelData = new int[8,8]{
		{0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0}
	};
	
	void Start () {
		Generate();
		
	}
	
	void Generate(){
		//picks a random location for the level to start and the second room, then generates a random path to the bottom of the level
		int LevelStartX = Random.Range(0,8);
		int LevelStartY = Random.Range(0,5);
		int LevelX = 0;
		int LevelY = 0;
		LevelData[LevelStartX,LevelStartY] = 1;
		Debug.Log(LevelStartX);
		Debug.Log(LevelStartY);
		int Direction = Random.Range(0,3);
		if(Direction==0){
			if(LevelStartX==0){
				LevelData[LevelStartX+1,LevelStartY]=1;
				LevelX = LevelStartX+1;
				LevelY = LevelStartY;
			}else{
				LevelData[LevelStartX-1,LevelStartY]=1;
				LevelX = LevelStartX-1;
				LevelY = LevelStartY;
			}
		}else if(Direction==1){
			if(LevelStartX==7){
				LevelData[LevelStartX-1,LevelStartY]=1;
				LevelX = LevelStartX-1;
				LevelY = LevelStartY;
			}else{
				LevelData[LevelStartX+1,LevelStartY]=1;
				LevelX = LevelStartX+1;
				LevelY = LevelStartY;
			}
		}else{
			LevelData[LevelStartX,LevelStartY+1]=1;
			LevelX = LevelStartX;
			LevelY = LevelStartY+1;
		}
		
		while(LevelY!=7){
			int NewDirection = Random.Range(0,3);
			if(NewDirection==0){
				if(LevelX!=0){
					if(LevelData[LevelX-1,LevelY]==0){
						LevelData[LevelX-1,LevelY]=1;
						LevelX=LevelX-1;
					}
				}
			}else if(NewDirection==1){
				if(LevelX!=7){
					if(LevelData[LevelX+1,LevelY]==0){
						LevelData[LevelX+1,LevelY]=1;
						LevelX=LevelX+1;
					}
				}
			}
			else if(NewDirection==2){
				if(LevelY!=7){
					if(LevelData[LevelX,LevelY-1]==0){
						LevelData[LevelX,LevelY+1]=1;
						LevelY=LevelY+1;
					}
				}
			}
		}
	}
	
}

After debugging using breakpoints, I’ve realized that the error was one of the checks done in RandomPath if NewDirection equals two. Essentially, the program would get stuck in a corner. However, I have fixed it by changing the check from if(LevelData[LevelX,LevelY-1]==0) to if(LevelData[LevelX,LevelY-1]==0 || LevelData[LevelX,LevelY-1]!=counter-1)