i use this script to create a maze. the problem is that the left and bottom outer wall is double. it has two walls on the outer edge. but it should be one wall like in the right and top sides.
i can’t find how to fix it. i added a screenshot image show in the scene view the maze and the problem on the maze left and bottom sides double walls.
this method builds the maze:
void BuildMaze()
{
// Clear previous walls
if (wallParent != null)
{
foreach (Transform child in wallParent.transform)
{
Destroy(child.gameObject);
}
}
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
// Skip inner cells where walls shouldn't exist
if (!maze[x, y]) continue;
// Correctly create walls for all valid cells and outer boundaries
Vector3 position = new Vector3(x, 0, y);
GameObject wall = Instantiate(wallPrefab, position, Quaternion.identity, wallParent.transform);
wall.transform.localScale = new Vector3(1, 2, 1);
wall.name = "Wall";
wall.tag = "Wall";
}
}
}
i tried to change it to this:
void BuildMaze()
{
// Clear previous walls
if (wallParent != null)
{
foreach (Transform child in wallParent.transform)
{
Destroy(child.gameObject);
}
}
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
// Skip inner cells where walls shouldn't exist
if (!maze[x, y]) continue;
// Correctly create walls for all valid cells and outer boundaries
if (x == 0 || x == width - 1 || y == 0 || y == height - 1)
{
// Ensure the outer walls are placed once
if ((x == width - 1 && y == height - 1) ||
(x == width - 1 && y == 0) ||
(x == 0 && y == height - 1)) continue;
}
Vector3 position = new Vector3(x, 0, y);
GameObject wall = Instantiate(wallPrefab, position, Quaternion.identity, wallParent.transform);
wall.transform.localScale = new Vector3(1, 2, 1);
wall.name = "Wall";
wall.tag = "Wall";
}
}
}
but then this make am ess on the maze. the top left bottom left and bottom right corners a missing wall and it’s still double walls on left and bottom.
this screenshot image shows the problem after i tried to fix it: