Perfect Maze Generator error
Hi, everyone.
I'm trying to make a perfect maze generator in unity3d but I'm getting the object reference not set to an instance of an object error.
The script is also supposed to create cells and assign the walls to north/west/south/east but has only parented the cloned walls to GameObject wallRecord ('Maze' in the higherarchy) so far. I think i've made a mistake in the math somewhere.
Any help would hugely appreciated, thank you.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MazeManager : MonoBehaviour {
[System.Serializable]
//Cell Variables
public class Cell {
public bool visited;
public GameObject north;
public GameObject east;
public GameObject south;
public GameObject west;
public Cell (){
north = null;
east = null;
west = null;
south = null;
//End
}
}
public GameObject wall;
public int xsize = 11;
public int ysize = 7;
public int currentCell = 0;
private int totalcells = 0;
public Cell[] cells;
private float wallLength = 1f;
private Vector3 initialPos;
private GameObject wallRecord;
private GameObject tempWall;
// Use this for initialization
void Start () {
Createwall ();
}
void Createwall () {
wallRecord = new GameObject ();
wallRecord.name = "Maze";
initialPos = new Vector3 ((-xsize / 2) + wallLength / 2, 0.0f, (-ysize / 2) + wallLength / 2);
Vector3 myPos = initialPos;
//x-axis walls
for (int i = 0; i < ysize; i++) {
for (int j = 0; j <= xsize; j++) {
myPos = new Vector3 (initialPos.x + (j*wallLength) -wallLength / 2, 0.0f, initialPos.z + (i*wallLength) -wallLength / 2);
tempWall = Instantiate (wall, myPos, Quaternion.identity) as GameObject;
tempWall.transform.parent = wallRecord.transform;
}
}
//y-axis walls
for (int i = 0; i <= ysize; i++) {
for (int j = 0; j < xsize; j++) {
myPos = new Vector3 (initialPos.x + (j*wallLength), 0.0f, initialPos.z + (i*wallLength) -wallLength);
tempWall = Instantiate (wall, myPos, Quaternion.Euler (0.0f, 90.0f, 0.0f)) as GameObject;
tempWall.transform.parent = wallRecord.transform;
}
}
CreateCells ();
}
void CreateCells () {
GameObject[] allWalls;
int children = wallRecord.transform.childCount;
allWalls = new GameObject [children];
cells = new Cell[xsize * ysize];
int EWProcess = 0;
int childProcess = 0;
int termCount = 0;
//Gets all the children
for (int i = 0; i < children; i++) {
allWalls[i] = wallRecord.transform.GetChild(i).gameObject;
}
//Walls to Cells
if (termCount == xsize) {
for (int cellprocess = 0; cellprocess < cells.Length; cellprocess++) {
cells [cellprocess] = new Cell ();
cells [cellprocess].east = allWalls [EWProcess];
cells [cellprocess].south = allWalls [childProcess + (xsize + 1) * ysize];
if (termCount == xsize) {
EWProcess += 2;
termCount = 0;
}
else
EWProcess++;
termCount++;
childProcess++;
cells [cellprocess].west = allWalls [EWProcess];
cells [cellprocess].north = allWalls [(childProcess+ (xsize+1)*ysize) +xsize - 1];
}
}
CreateMaze ();
}
void CreateMaze () {
RecordNeighbour ();
}
void RecordNeighbour () {
totalcells = xsize * ysize;
int Length = 0;
int[] neigbours = new int[4];
int rec = 0;
rec = ((currentCell + 1) / xsize);
rec -= 1;
rec *= xsize;
rec += xsize;
//west
if (currentCell + 1 < totalcells && (currentCell) != rec) {
if (cells [currentCell + 1].visited == false) {
neigbours [Length] = currentCell + 1;
Length++;
}
}
//east
if (currentCell - 1 < totalcells && (currentCell) != rec) {
if (cells [currentCell - 1].visited == false) {
neigbours [Length] = currentCell - 1;
Length++;
}
}
//north
if (currentCell + xsize < totalcells) {
if (cells [currentCell + xsize].visited == false) {
neigbours [Length] = currentCell + xsize;
Length++;
}
}
//south
if (currentCell - xsize < totalcells) {
if (cells [currentCell - xsize].visited == false) {
neigbours [Length] = currentCell - xsize;
Length++;
}
}
for (int i = 0; i < Length; i++)
Debug.Log (neigbours [i]);
}
}
Comment