I have a maze generator script that is supposed to duplicate a prefab 4 wall cell then knock walls to make a maze and idk why its not working all the way, no errors or warning, it seems to be knocking a few ways so not enough to make a maze (see pics below)
Maze Generator script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MazeGenerator : MonoBehaviour {
[Range(5, 500)]
public int mazeWidth = 5, mazeHeight = 5;
public int startX, startY;
MazeCell[,] maze;
Vector2Int currentCell;
public MazeCell[,] GetMaze() {
maze = new MazeCell[mazeWidth, mazeHeight];
for (int x = 0; x < mazeWidth; x++) {
for (int y = 0; y < mazeHeight; y++) {
maze[x, y] = new MazeCell(x, y);
}
}
CarvePath(startX, startY);
return maze;
}
List<Direction> directions = new List<Direction> {
Direction.Up, Direction.Down, Direction.Left, Direction.Right,
};
List<Direction> GetRandomDirections() {
List<Direction> dir = new List<Direction>(directions);
List<Direction> rndDir = new List<Direction>();
while (dir.Count > 0) {
int rnd = Random.Range(0, dir.Count);
rndDir.Add(dir[rnd]);
dir.RemoveAt(rnd);
}
return rndDir;
}
bool IsCellValid (int x, int y) {
if(x < 0 || y < 0 || x > mazeWidth - 1 || y > mazeHeight - 1 || maze[x, y].visited) return false;
else return true;
}
Vector2Int CheckNeighbours() {
List<Direction> rndDir = GetRandomDirections();
for (int i = 0; i < rndDir.Count; i++) {
Vector2Int neighbour = currentCell;
switch (rndDir*) {
case Direction.Up:
neighbour.y++;
break;
case Direction.Down:
neighbour.y--;
break;
case Direction.Right:
neighbour.x++;
break;
case Direction.Left:
neighbour.x--;
break;
}
if (IsCellValid(neighbour.x, neighbour.y)) return neighbour;
}
return currentCell;
}
void BreakWalls (Vector2Int primaryCell, Vector2Int secondaryCell) {
if (primaryCell.x > secondaryCell.x) {
maze[primaryCell.x, primaryCell.y].leftWall = false;
} else if (primaryCell.x < secondaryCell.x) {
maze[secondaryCell.x, secondaryCell.y].leftWall = false;
} else if (primaryCell.y < secondaryCell.y) {
maze[primaryCell.x, primaryCell.y].topWall = false;
} else if (primaryCell.y < secondaryCell.y) {
maze[secondaryCell.x, secondaryCell.y].topWall = false;
}
}
void CarvePath (int x, int y) {
if (x < 0 || y < 0 || x > mazeWidth - 1 || y > mazeHeight -1) {
x = y = 0;
Debug.LogWarning("Starting position is out of bounds, defaulting to 0, 0");
}
currentCell = new Vector2Int(x, y);
List<Vector2Int> path = new List<Vector2Int>();
bool deadEnd = false;
while (!deadEnd) {
Vector2Int nextCell = CheckNeighbours();
if (nextCell == currentCell) {
for (int i= path.Count - 1; i >= 0; i--) {
currentCell = path;
path.RemoveAt(i);
nextCell = CheckNeighbours();
if(nextCell != currentCell) break;
}
if (nextCell != currentCell)
deadEnd = true;
} else {
BreakWalls(currentCell, nextCell);
maze[currentCell.x, currentCell.y].visited = true;
currentCell = nextCell;
path.Add(currentCell);
}
}
}
}
public enum Direction {
Up,
Down,
Left,
Right,
}
public class MazeCell {
public bool visited;
public int x, y;
public bool topWall;
public bool leftWall;
public Vector2 position {
get {
return new Vector2Int(x, y);
}
}
public MazeCell (int x, int y) {
this.x = x;
this.y = y;
visited = false;
topWall = leftWall = true;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MazeCellObject : MonoBehaviour
{
[SerializeField] GameObject topWall;
[SerializeField] GameObject bottomWall;
[SerializeField] GameObject rightWall;
[SerializeField] GameObject leftWall;
public void Init (bool top, bool bottom, bool right, bool left)
{
topWall.SetActive(top);
bottomWall.SetActive(bottom);
rightWall.SetActive(right);
leftWall.SetActive(left);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MazeRenderer : MonoBehaviour
{
[SerializeField] MazeGenerator mazeGenerator;
[SerializeField] GameObject MazeCellPrefab;
// This the physical size of our maze cells. Getting this wrong will result in overlapping
// or visible gaps between each cell.
public float CellSize = 1f;
private void Start() {
MazeCell[,] maze = mazeGenerator.GetMaze();
for (int x = 0; x < mazeGenerator.mazeWidth; x++) {
for (int y = 0; y < mazeGenerator.mazeHeight; y++) {
GameObject newCell = Instantiate(MazeCellPrefab, new Vector3((float)x * CellSize, 0f, (float)y * CellSize), Quaternion.identity, transform);
MazeCellObject mazeCell = newCell.GetComponent<MazeCellObject>();
bool top = maze[x, y].topWall;
bool left = maze[x, y].leftWall;
bool right = false;
bool bottom = false;
if (x == mazeGenerator.mazeWidth -1) right = true;
if (y == 0) bottom = true;
mazeCell.Init(top, bottom, right, left);
}
}
}
}

