- Home /
How do I check if a spot in a cell is missing?
Hi, I am trying to find out if a wall in a maze is broken ( in generation). the maze is divided into cells. I want to look into a cell, for example, cell 0, and check each wall in the cell to see if it's not there. If there is no game object set, north, for example ( I have, North, East, South, and West) I want to be able to debug.log that there is no north. I have had no luck thus far.
Here is what I have so far.
{
Debug.Log("started route");
if(cells[0].north = null)
{
Debug.Log("no north");
}
if (cells[0].east = null)
{
Debug.Log("no east");
}
}
I am using this to try and find a path to the exit with code. NOTES: the maze is randomly generated through the code, so I cant modify the walls beforehand. Currently, if I look into a cell with a wall missing it says, NORTH: Missing
Answer by Papnix · Mar 28, 2018 at 04:14 PM
Hi, very quick answer. You don't test anything with cells[0].north = null (one single = is an assignation). You need == to test something. if(cells[0].north == null) would be better.
Thanks for the quick reply, I tried the code with == ins$$anonymous$$d of =, and I am not getting any return =/. I am testing for null currently,
if(cells[0].north == null) Do you know if null works for missing game objects too? because I am not getting the debug =/
thanks for the help
How do you initialize your cells ? Can I see your code where you fill it ?
$$anonymous$$y code is posted as an answer so you can see it better,
Answer by quadack · Mar 28, 2018 at 04:33 PM
Here is the part of the code where I make the walls from prefabs and put them into cells based on position and orientation.
public Walls()
{
intposition = new Vector3(-data.xsize / 2, 0.0f, (-data.ysize / 2) + (wallLength/2));
Debug.Log(intposition);
Vector3 myposition = intposition;
GameObject tempWall;
wallfolder = new GameObject();
wallfolder.name = ("wall folder");
cells = new cell[data.xsize * data.ysize];
//for bottom plane
//for x axis
for (int i = 0; i < data.ysize; i++)
{
for (int j = 0; j <= data.xsize; j++)
{
myposition = new Vector3(intposition.x + (j * wallLength) - wallLength / 2, 0.0f, intposition.z + (i * wallLength) - wallLength / 2);
tempWall = Instantiate(wall, myposition, Quaternion.identity) as GameObject;
tempWall.transform.parent = wallfolder.transform;
totalwallcolor++;
}
}
//for y axis
for (int i = 0; i <= data.ysize; i++)
{
for (int j = 0; j < data.xsize; j++)
{
myposition = new Vector3(intposition.x + (j * wallLength) , 0.0f, intposition.z + (i * wallLength) - wallLength );
tempWall = Instantiate(wall, myposition, Quaternion.Euler(0f, 90f, 0f)) as GameObject;
tempWall.transform.parent = wallfolder.transform;
totalwallcolor++;
}
}
CreateCells();
}
public void CreateCells()
{
lastCells = new List<int>();
lastCells.Clear();
totalcells = data.xsize * data.ysize;
GameObject[] AllWalls;
int children = wallfolder.transform.childCount;
AllWalls = new GameObject[children];
cells = new cell[data.xsize * data.ysize];
int eastWestProcess = 0;
int childProcess = 0;
int termcount = 0;
//get the int of children
for (int i = 0; i < children; i++ )
{
AllWalls[i] = wallfolder.transform.GetChild(i).gameObject;
}
//assign walls to the cells
for (int cellprocess = 0; cellprocess < cells.Length; cellprocess++)
{
if (termcount == data.xsize)
{
eastWestProcess++;
termcount = 0;
}
cells[cellprocess] = new cell();
cells[cellprocess].east = AllWalls[eastWestProcess];
cells[cellprocess].south = AllWalls[childProcess + (data.xsize + 1) * data.ysize];
eastWestProcess++;
termcount++;
childProcess++;
cells[cellprocess].west = AllWalls[eastWestProcess];
cells[cellprocess].north = AllWalls[(childProcess
+ (data.xsize + 1) * data.ysize) + data.xsize -1];
}
CreateMaze();
//test12
}
Then I apply a DFS to make the maze.
Sorry mate, I didn't quite understand what you are doing and where it's wrong. I don't know if it helps but your array cells is declared two times (line 10 and line 51). Try to print what's in it to see if it is correctly filled.
Your answer
![](https://koobas.hobune.stream/wayback/20220612155542im_/https://answers.unity.com/themes/thub/images/avi.jpg)