How to fix the maze creation where the left and bottom sides of the grid have double walls instead one wall?

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:

The maze width and height needs to be an odd number. So for a 50x50 maze specify 49x49 instead.

1 Like

working. thank you.

1 Like

and if i want to make it possible to put in the inspector 50,50 and it will size of 50,50 ? or any other size 30,30 or 29,29 and not only odd numbers like 49,49 to get 50,50 size. how to do it ?

You can have your script still accept even values but internally it should tweak them to be odd. So 50x50 becomes 49x49 or 51x51, whichever you prefer. Add private realWidth and realHeight variables.