Need help on a little script.

I’m making a map generator. To generate i need to know which directions are available to generate to. Then i need to, at random, pick one of the available directions.

I can find which directions are available, but i cant figure out a way to choose one at random.
One solution that works.

     if (currentMapSize < mapSize) {

        //imagine if there is code here that finds available directions...
        
        int direction = 0;
		int availableDirections = x; // x stands for available directions that were found
		bool foundDir;
		
		for (int i=0;foundDir == false; i++) {
			int randomInt = Random(1,availableDirections+1);
			if (randomInt == 1 && canUp) {
				foundDir = true;
			}
			if (randomInt == 2 && canDown) {
				foundDir = true;
			}
			if (randomInt == 3 && canRight) {
				foundDir = true;
			}
			if (randomInt == 4 && canLeft) {
				foundDir = true;
			}
		}
    }

But the problem with this code, is that it sometimes crashes unity. I have no idea why, nor do i know how to fix it. So i am asking for your help to solve this problem.

Full code:

public class MapGen : MonoBehaviour {

public GameObject[] room;
public List<RoomPiece> Map = new List<RoomPiece>();

public int mapSize = 3;
public int currentSize;
public int currentBranchPopulation;
public int branchpopulation = 6;

public Vector2 pos = new Vector2 (0,0);
bool[,] fixedPos = new bool[600,600];

public int iposx;
public int iposy;

void Start () {
	fixedPos[(int)(pos.x+300),(int)(pos.y+300)] = true;
}

void Update () {
	//MainBranch
	if (currentSize < mapSize) {
		currentSize ++;

		//Get available directions
		bool canUp = true, canDown = true, canRight = true, canLeft = true;
		iposx = (int)pos.x + 300;
		iposy = (int)pos.y + 300;

		int availableSides = 0;
		if (fixedPos[iposx,iposy+ 1]) {
			canUp = false;
			availableSides++;
		}
		if (fixedPos[iposx,iposy - 1]) {
			canDown = false;
			availableSides++;
		}
		if (fixedPos[iposx + 1,iposy]) {
			canRight = false;
			availableSides++;
		}
		if (fixedPos[iposx - 1,iposy]) {
			canLeft = false;
			availableSides++;
		}

		if (!canUp && !canDown && !canRight && !canLeft) {
			print("end branch");
		}

		bool up = false, down = false, right = false, left = false;
		bool foundPath = false;

                    //Sets new path/direction
		while (!foundPath) {
			int dir = Random.Range(1,5);
			if (dir == 1 && canUp) {
				pos = new Vector2 (pos.x, pos.y + 1);
				up = true;
				foundPath = true;
			}
			if (dir==2 && canDown) {
				pos = new Vector2 (pos.x, pos.y - 1);
				down = true;
				foundPath = true;
			}
			if (dir==3 && canRight) {
				pos = new Vector2 (pos.x + 1, pos.y);
				right = true;
				foundPath = true;
			}
			if (dir==4 && canLeft) {
				pos = new Vector2 (pos.x - 1, pos.y);
				left = true;
				foundPath = true;
			}
		}

		string name1 = "Room " + currentSize;
		Map.Add(new RoomPiece(name1 ,pos, up, down, right, left));
		Spawn(name1 ,pos,up,down,right,left);
	} else {
                    //You can ignore this, this is almost the same as above.
		if (currentBranchPopulation < branchpopulation) {
			currentBranchPopulation++;

			int random = Random.Range(0,mapSize);
			Vector2 randomRoom = Map[random].position;

			bool canUp = true, canDown = true, canRight = true, canLeft = true;
			iposx = (int)randomRoom.x + 300;
			iposy = (int)randomRoom.y + 300;

			int availableSides = 0;
			if (fixedPos[iposx,iposy+ 1]) {
				canUp = false;
				availableSides++;
			}
			if (fixedPos[iposx,iposy - 1]) {
				canDown = false;
				availableSides++;
			}
			if (fixedPos[iposx + 1,iposy]) {
				canRight = false;
				availableSides++;
			}
			if (fixedPos[iposx - 1,iposy]) {
				canLeft = false;
				availableSides++;
			}

			bool up = false, down = false, right = false, left = false;

			int dir = Random.Range(1,availableSides + 1);
				
			if (dir == 1 && canUp) {
				pos = new Vector2 (randomRoom.x, randomRoom.y + 1);
				up = true;
			}
			if (dir==2 && canDown) {
				pos = new Vector2 (randomRoom.x, randomRoom.y - 1);
				down = true;
			}
			if (dir==3 && canRight) {
				pos = new Vector2 (randomRoom.x + 1, randomRoom.y);
				right = true;
			}
			if (dir==4 && canLeft) {
				pos = new Vector2 (randomRoom.x - 1, randomRoom.y);
				left = true;
			}

			string name1 = "Branch " + currentBranchPopulation;
			Map.Add(new RoomPiece(name1 ,pos, up, down, right, left));
			Spawn(name1,pos,up,down,right,left);
		}
	}

}

    //This spawns the actual blocks/levels/map however you wanna call it.
void Spawn (string cur,Vector2 pos, bool up, bool down, bool right, bool left) {
	Quaternion rotation = Quaternion.identity;
	Vector3 position = new Vector3(pos.x * 11, 0, pos.y * 11);
	fixedPos[(int)(pos.x+300),(int)(pos.y+300)] = true;

	if (up) {
		rotation.eulerAngles = new Vector3(0,0,0);
	}
	if (down) {
		rotation.eulerAngles = new Vector3(0,180,0);
	}
	if (right) {
		rotation.eulerAngles = new Vector3(0,90,0);
	}
	if (left) {
		rotation.eulerAngles = new Vector3(0,-90,0);
	}

	GameObject r = Instantiate (room[0], position, rotation) as GameObject;
	r.gameObject.name = cur;

}

}

OK, i made a huge mistake and didn’t notice it.
When i checked if it can go up/down/right/left. If it couldnt, i added the line AvailableSides++; When it shoudve only added them if could.
So the problem was, when it couldn’t find any direction to go to, it just stayed in while loop for infinite amount, so unity crashed.