I need help making variable size cells in my maze generator

I have a maze generation script that I got by watching this video:
Maze Generation by Ketra Games

Excellent breakdown, however, every time I try to change the scale of my maze cells the maze generation breaks. As you see in my script I am wanting to be able to pass another number as a cellSize and the cells increase by that amount … 2 = currentCellSize * 2, etc.

Any help is greatly appreciated!

Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class MazeGeneratorWithProgress : MonoBehaviour
{
[SerializeField] private List _mazeCellPrefabs;

[SerializeField] private int _mazeWidth = 20;

[SerializeField] private int _mazeDepth = 20;

[SerializeField] private int _cellSize = 1;

[SerializeField] private int _seed;

[SerializeField] private bool _useSeed;

private MazeCell[,] _mazeGrid;

// Start is called before the first frame update
IEnumerator Start()
{
    if (_useSeed)
    {
        Random.InitState(_seed); // Use Seed to reproduce the same maze
    }
    else
    {
        int randomSeed = (int)Random.Range(1,999999999999999);
        Random.InitState(randomSeed);
        Debug.Log(randomSeed);
    }

    //Choose Random Prefab
    int prefabIndex = Random.Range(0, _mazeCellPrefabs.Count);
    MazeCell _mazeCellPrefab = _mazeCellPrefabs[prefabIndex];

    _mazeGrid = new MazeCell[_mazeWidth, _mazeDepth];

    for (int x = 0; x < _mazeWidth; x++)
    {
        for (int z = 0; z < _mazeDepth; z++)
        {
            _mazeGrid[x, z] = Instantiate(_mazeCellPrefab, new Vector3(x, 0, z), Quaternion.identity, transform);
            _mazeGrid[x, z].SetScale(_cellSize);
        }
    }

    yield return GenerateMaze(null, _mazeGrid[0,0]);
}

private IEnumerator GenerateMaze(MazeCell previousCell, MazeCell currentCell)
{
    currentCell.Visit(true);
    ClearWalls(previousCell, currentCell);

    yield return new WaitForSeconds(0.05f);

    MazeCell nextCell;

    do
    {
        nextCell = GetNextUnvisitedCell(currentCell);

        if (nextCell != null)
        {
            yield return GenerateMaze(currentCell, nextCell);
        }
    } while (nextCell != null);
}

private MazeCell GetNextUnvisitedCell(MazeCell currentCell)
{
    var unvisitedCells = GetUnvisitedCells(currentCell);

    return unvisitedCells.OrderBy(_ => Random.Range(1, 10)).FirstOrDefault();
}

private IEnumerable<MazeCell> GetUnvisitedCells(MazeCell currentCell)
{
    int x = (int)currentCell.transform.position.x;
    int z = (int)currentCell.transform.position.z;

    // Cell to the Right
    if(x + 1 < _mazeWidth)
    {
        var cellToRight = _mazeGrid[x + 1, z];

        if(cellToRight.IsVisited == false)
        {
            yield return cellToRight;
        }
    }

    // Cell to the Left
    if (x - 1 >= 0)
    {
        var cellToLeft = _mazeGrid[x - 1, z];

        if (cellToLeft.IsVisited == false)
        {
            yield return cellToLeft;
        }
    }

    // Cell to the Front
    if (z + 1 < _mazeDepth)
    {
        var cellToFront = _mazeGrid[x, z + 1];

        if (cellToFront.IsVisited == false)
        {
            yield return cellToFront;
        }
    }

    // Cell to the Back
    if (z - 1 >= 0)
    {
        var cellToBack = _mazeGrid[x, z - 1];

        if (cellToBack.IsVisited == false)
        {
            yield return cellToBack;
        }
    }
}

private void ClearWalls(MazeCell previousCell, MazeCell currentCell)
{
    if(previousCell == null)
    {
        return; // No Walls to clear, 1st Cell
    }

    if(previousCell.transform.position.x < currentCell.transform.position.x)
    {
        //Moved from Left to Right
        previousCell.ClearRightWall(); // Clear Previous Right
        currentCell.ClearLeftWall(); // Clear Current Left
        return;
    }

    if (previousCell.transform.position.x > currentCell.transform.position.x)
    {
        //Moved from Right to Left
        previousCell.ClearLeftWall(); // Clear Previous Right
        currentCell.ClearRightWall(); // Clear Current Left
        return;
    }

    if (previousCell.transform.position.z < currentCell.transform.position.z)
    {
        //Moved from Back to Front
        previousCell.ClearFrontWall(); // Clear Previous Front
        currentCell.ClearBackWall(); // Clear Current Back
        return;
    }

    if (previousCell.transform.position.z > currentCell.transform.position.z)
    {
        //Moved from Front to Back
        previousCell.ClearBackWall(); // Clear Previous Back
        currentCell.ClearFrontWall(); // Clear Current Front
        return;
    }
}

// Update is called once per frame
void Update()
{
    
}

}

Maze Cell Script:
using UnityEngine;

public class MazeCell : MonoBehaviour
{
[SerializeField] GameObject _frontWall;
[SerializeField] GameObject _backWall;
[SerializeField] GameObject _leftWall;
[SerializeField] GameObject _rightWall;
[SerializeField] GameObject _floor;
[SerializeField] GameObject _ceiling;

public bool IsVisited { get; private set; }

public bool HasPlayerVisited { get; private set; }

public bool PlayerStanding { get; private set; }

public void Visit(bool clearCeiling = false)
{
    IsVisited = true;

    if (clearCeiling)
    {
        ClearCeiling();
    }
}

private void Start()
{
    Collider _cellCollider = GetComponent<Collider>();
    _cellCollider.isTrigger = true;
}

private void OnTriggerEnter(Collider collision)
{
    if (collision.tag == "Player")
    {
        HasPlayerVisited = true;
        PlayerStanding = true;
    }
}

private void OnTriggerExit(Collider collision)
{
    if (collision.tag == "Player")
    {
        PlayerStanding = false;
    }
}

public void SetScale(int cellSize)
{
    if(cellSize < 1)
    {
        cellSize = 1; // Safety Check
    }

    _leftWall.transform.position = new Vector3(_leftWall.transform.position.x * cellSize, 0, 0);
    _leftWall.transform.localScale
        = new Vector3(_leftWall.transform.localScale.x, 0f, _leftWall.transform.localScale.z * cellSize);

    _rightWall.transform.position = new Vector3(_rightWall.transform.position.x * cellSize, 0, 0);
    _rightWall.transform.localScale
        = new Vector3(_rightWall.transform.localScale.x, 0f, _rightWall.transform.localScale.z * cellSize);

    _frontWall.transform.position = new Vector3(0, 0, _frontWall.transform.position.z * cellSize);
    _frontWall.transform.localScale
        = new Vector3(_frontWall.transform.localScale.x * cellSize, 0f, _frontWall.transform.localScale.z);

    _backWall.transform.position = new Vector3(0, 0, _backWall.transform.position.z * cellSize);
    _backWall.transform.localScale
        = new Vector3(_backWall.transform.localScale.x * cellSize, 0f, _backWall.transform.localScale.z);


    _ceiling.transform.localScale
        = new Vector3(_ceiling.transform.localScale.x * cellSize, 0f, _ceiling.transform.localScale.z * cellSize);


    _floor.transform.localScale
        = new Vector3(_floor.transform.localScale.x * cellSize, 0f, _floor.transform.localScale.z * cellSize);
}

public void ClearLeftWall()
{
    _leftWall.SetActive(false);
}

public void ClearRightWall()
{
    _rightWall.SetActive(false);
}

public void ClearFrontWall()
{
    _frontWall.SetActive(false);
}

public void ClearBackWall()
{
    _backWall.SetActive(false);
}

public void ClearCeiling()
{
    _ceiling.SetActive(false);
}

public void ClearFloor()
{
    _floor.SetActive(false);
}

public void PlayerEntered()
{
    HasPlayerVisited = true;
}

}

Wild to see your question here after I just recently completed the exact same tutorial and also needed to increase the scale of my maze. :slight_smile:

My approach was a little simpler than yours and seems to give me good results. I apply the cell scaling to the entire cell prefab at instantiation and then adjust my neighboring checks to use the cell scale as well.

Here’s my working script.

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

public class MazeGenerator : MonoBehaviour
{

    [SerializeField]
    private MazeCell mazeCell;

    [SerializeField]
    private int mazeWidth, mazeDepth;

    private MazeCell[,] mazeGrid;

    [SerializeField]
    private int mazeScale = 1;

    IEnumerator Start()
    {
        mazeGrid = new MazeCell[mazeWidth, mazeDepth];

        for (int x = 0; x < mazeWidth; x+=mazeScale)
        {
            for (int z = 0; z < mazeDepth; z+=mazeScale)
            {
                var cell = Instantiate(mazeCell, transform, true);
                cell.transform.localScale = Vector3.one * mazeScale;
                cell.transform.localPosition = new(x, 0, z);
                mazeGrid[x, z] = cell;
            }
        }

        yield return GenerateMaze(null, mazeGrid[0, 0]);
    }

    private IEnumerator GenerateMaze(MazeCell previous, MazeCell current)
    {
        current.Visit();
        ClearWalls(previous, current);

        yield return new WaitForSeconds(0.05f);

        MazeCell nextCell;

        do
        {
            nextCell = GetNextUnvisitedCell(current);
            if (nextCell != null)
            {
                yield return GenerateMaze(current, nextCell);
            }
        } while (nextCell != null);
    }

    private MazeCell GetNextUnvisitedCell(MazeCell cell)
    {
        var unvisited = GetUnvisitedNeighbors(cell);

        return unvisited.OrderBy(_ => Random.Range(1, 10)).FirstOrDefault();
    }

    private IEnumerable<MazeCell> GetUnvisitedNeighbors(MazeCell cell)
    {
        int x = (int)cell.transform.localPosition.x;
        int z = (int)cell.transform.localPosition.z;

        if (x + mazeScale < mazeWidth)
        {
            var cellToRight = mazeGrid[x + mazeScale, z];

            if (cellToRight.IsVisited == false)
            {
                yield return cellToRight;
            }
        }

        if (x - mazeScale >= 0)
        {
            var cellToLeft = mazeGrid[x - mazeScale, z];

            if (cellToLeft.IsVisited == false)
            {
                yield return cellToLeft;
            }
        }

        if (z - mazeScale >= 0)
        {
            var cellAbove = mazeGrid[x, z - mazeScale];

            if (cellAbove.IsVisited == false)
            {
                yield return cellAbove;
            }
        }

        if (z + mazeScale < mazeDepth)
        {
            var cellBelow = mazeGrid[x, z + mazeScale];

            if (cellBelow.IsVisited == false)
            {
                yield return cellBelow;
            }
        }

    }

    private void ClearWalls(MazeCell previous, MazeCell current)
    {
        if (previous == null)
        {
            return;
        }

        if (previous.transform.position.x < current.transform.position.x)
        {
            previous.ClearRightWall();
            current.ClearLeftWall();
            return;
        }

        if (previous.transform.position.x > current.transform.position.x)
        {
            previous.ClearLeftWall();
            current.ClearRightWall();
            return;
        }

        if (previous.transform.position.z < current.transform.position.z)
        {
            previous.ClearTopWall();
            current.ClearBottomWall();
            return;
        }

        if (previous.transform.position.z > current.transform.position.z)
        {
            previous.ClearBottomWall();
            current.ClearTopWall();
            return;
        }
    }

    // Update is called once per frame
    void Update()
    {

    }
}

DUDE! That is brilliant! Thank you so much!! … and super crazy we both just finished the same tutorial LOL