- Home /
Found the typo
GameObject not being Destroyed (I've Searched...!)
Okay, so this code was much smaller, but I kept thinking I knew what the problem was and appending it until it became slightly monstrous. So, I generate a grid of cells with another code, adding each to a "cells" 2d array. Each cell has only the top and right wall unless they are on the left or bottom edge of the grid. That code then passes the "Cells" array as well as the array's dimensions to the code below. This is what the code is supposed to do: The "Search" function iterates through each cell and sees if it has been added to the "path" (tracked by the mirrored "pathed" 2d array). If it has not, the "Path" function should link it to a random nearby, already pathed cell and then move out in random diractions, adding cells to the path and updating their status in "pathed". Where I am running into trouble: The random generation seems to work okay when I track it in debug, but the walls which I specify seem to only be deleted by the "linking" section of the function, and not by the "pathing" section of the function. Don't be snide, please. I'm tired. Here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SearchPath : MonoBehaviour { public static GameObject NorthWall; public static GameObject SouthWall; public static GameObject EastWall; public static GameObject WestWall; public static GameObject Floor; public static bool[,] Pathed; public static bool[,] Linked;
public static void Search(GameObject[,] Cells)
{
Linked = new bool[Cells.GetLength(0), Cells.GetLength(1)];
Pathed = new bool[Cells.GetLength(0), Cells.GetLength(1)];
//Set Bools in mirror grid to false
for (int X = 0; X < Cells.GetLength(0); X++)
{
for (int Z = 0; Z < Cells.GetLength(1); Z++)
{
Pathed[X, Z] = false;
}
}
Pathed[0,0] = true;
//set bools in Linked grid to false
for (int X = 0; X < Cells.GetLength(0); X++)
{
for (int Z = 0; Z < Cells.GetLength(1); Z++)
{
Linked[X, Z] = false;
}
}
Linked[0, 0] = true;
//Search through grid
for (int X = 0; X < Cells.GetLength(0); X++)
{
for (int Z = 0; Z < Cells.GetLength(1); Z++)
{
print("Checking if" + X + " " + Z + " has been pathed");
if (X == 0 && Z == 0)
{
Path(0, 0, Cells);
}
else if (Pathed[X, Z] == false)
{
Pathed[X, Z] = true;
}
else
{
print("it has been pathed, moving on");
Path(X, Z, Cells);
}
}
}
}
private static void Path(int X, int Z, GameObject[,] Cells)
{
//Only add walls or bordering cells if they exist and are not pathed (circumstantially) Then delete walls as necessary
NorthWall = null;
SouthWall = null;
EastWall = null;
WestWall = null;
//North
if (Cells[X, Z].transform.Find("North").gameObject != null)
{
NorthWall = Cells[X, Z].transform.Find("North").gameObject;
}
else
{
NorthWall = Cells[X, Z + 1].transform.Find("South").gameObject;
}
//East
if (Cells[X, Z].transform.Find("East").gameObject != null)
{
EastWall = Cells[X, Z].transform.Find("East").gameObject;
}
else
{
EastWall = Cells[X + 1, Z].transform.Find("West").gameObject;
}
//South
if (Cells[X, Z].transform.Find("South").gameObject != null)
{
SouthWall = Cells[X, Z].transform.Find("South").gameObject;
}
else
{
SouthWall = Cells[X, Z - 1].transform.Find("North").gameObject;
}
//West
if (Cells[X, Z].transform.Find("West").gameObject != null)
{
WestWall = Cells[X, Z].transform.Find("West").gameObject;
}
else
{
WestWall = Cells[X - 1, Z].transform.Find("East").gameObject;
}
//First connect cell to maze
if (Pathed[X, Z] == false)
{
print("linking " + X + ", " + Z);
bool connected = false;
while (!connected)
{
int rando = (Mathf.CeilToInt(Random.Range(0.0001f, 4)) - 1);
print(rando);
switch (rando)
{
case 3:
if (Z != Cells.GetLength(1) - 1)
{
if (Pathed[X, Z + 1] == true)
{
Destroy(NorthWall);
connected = true;
Pathed[X, Z] = true;
print("delete to link");
}
}
break;
case 1:
if (Z != 0)
{
if (Pathed[X, Z - 1] == true)
{
Destroy(SouthWall);
connected = true;
Pathed[X, Z] = true;
print("delete to link");
}
}
break;
case 0:
if (X != Cells.GetLength(0) - 1)
{
if (Pathed[X + 1, Z] == true)
{
Destroy(EastWall);
connected = true;
Pathed[X, Z] = true;
print("delete to link");
}
}
break;
case 2:
if (X != 0)
{
if (Pathed[X - 1, Z] == true)
{
Destroy(WestWall);
connected = true;
Pathed[X, Z] = true;
print("delete to link");
}
}
break;
default:
print("something wrong with link");
connected = true;
break;
}
}
}
//See if Can Path
bool canPath = false;
if (X != 0)
{
if (Pathed[X - 1, Z] == false)
{
canPath = true;
}
}
if (Z != 0)
{
if (Pathed[X, Z - 1] == false)
{
canPath = true;
}
}
if (X != Cells.GetLength(0) - 1)
{
if (Pathed[X + 1, Z] == false)
{
canPath = true;
}
}
if (Z != Cells.GetLength(1) - 1)
{
if (Pathed[X, Z + 1] == false)
{
canPath = true;
}
}
if (canPath == true) {
print("Can Continue path");
}
else
{
print("couldn't path");
}
//Stop if top right tile
if (X == Cells.GetLength(0) - 1 && Z == Cells.GetLength(1) - 1)
{
canPath = false;
print("nevermind, maze end");
}
if (canPath == true)
{
bool moved = false;
while (!moved)
{
int rando = (Mathf.CeilToInt(Random.Range(0.0001f, 4)) - 1);
print(rando + " for cutPath");
switch (rando)
{
case 0:
if (X != Cells.GetLength(0) - 1)
{
if (Pathed[X + 1, Z] == false)
{
Pathed[X + 1, Z] = true;
Destroy(EastWall);
moved = true;
X++;
print("delete to PATH");
}
}
break;
case 2:
if (X != 0)
{
if (Pathed[X - 1, Z] == false)
{
Pathed[X - 1, Z] = true;
Destroy(WestWall);
moved = true;
X--;
print("delete to PATH");
}
}
break;
case 3:
if (Z != Cells.GetLength(1) - 1)
{
if (Pathed[X, Z + 1] == false)
{
Pathed[X, Z + 1] = true;
Destroy(NorthWall);
moved = true;
Z++;
print("delete to PATH");
}
}
break;
case 1:
if (Z != 0)
{
if (Pathed[X, Z - 1] == false)
{
Pathed[X, Z - 1] = true;
Destroy(SouthWall);
moved = true;
Z--;
print("delete to PATH");
}
}
break;
default:
break;
}
}
if (moved == true)
{
print("moving to " + X + " " + Z);
Path(X, Z, Cells);
}
}
}
}
Here is my gridmaker class. Each cell is equipped with four walls and a floor on instantiation.
public class Grid$$anonymous$$aker : $$anonymous$$onoBehaviour { private GameObject CellPrefab; private GameObject Cell; private GameObject NorthWall; private GameObject SouthWall; private GameObject EastWall; private GameObject WestWall; private GameObject Floor; private GameObject[,] Cells; // Start is called before the first frame update void Awake() { CellPrefab = Resources.Load("Art/Prefabs/Architecture/Cell") as GameObject; }
void Update()
{
}
public GameObject[,] $$anonymous$$akeGrid(int height, int width) {
Cells = new GameObject[width,height];
for (int x = 0; x < width; x++) {
for (int z = 0; z < width; z++)
{
Cell = Instantiate(CellPrefab, new Vector3(x * 6, 0, z * 6), new Quaternion(0, 0, 0, 0));
if (z != height - 1)
{
NorthWall = Cell.transform.Find("North").gameObject;
Destroy(NorthWall);
}
if (x != width - 1)
{
EastWall = Cell.transform.Find("East").gameObject;
Destroy(EastWall);
}
Cells[x,z] = Cell;
}
}
return Cells;
}
}
I seriously love you in advance
Answer by rh_galaxy · Oct 14, 2020 at 07:05 AM
I'm not sure, but whenever you destroy a wall, you should set your gameobject variable to null after. I haven't followed the recursive code enough so I don't know if you check/use the gameobjects any time after they are destroyed. If you do you will think they are valid when infact it has been or will be destroyed. So:
Destroy(EastWall); //this destroyes the gameObject later but not immediatly
EastWall = null; //so you don't think it exists later
Answer by Catonyourhead · Oct 14, 2020 at 04:59 AM
Bump. I feel as though my problem isn't with the logic, but with some way I'm calling a variable wrong or something... This is my third project ever, so there's a lot of gaps in my knowledge. EDIT: I have done more testing and when a cell doesn't have a north/south/etc wall it sets its wall to the cell next to it's inverse wall. at least that's waht's supposed to happen. It appears that my problem isn't the pathing, as with the debugs I can actually follow the logic around the maze and it should be working and making aperfect maze, but somehow, from printing the names of the walls it's trying to delete, I realize that it's trying TO DELETE WALLS THAT DON'T EXIST? I print the name of the wall, but shouldn't it print "null" if there is no wall there? I will seriously send you my project file if you can help. Here is a screenshot. This is the result of me printing the name of the wall. you can see, that wall doesn't actually exist. if I go around the editor deleting walls, I can see there is only one wall on each "edge/link." Please help!!!