- Home /
Instantiated gameObject(here, wall) destroyed, but when checked for existence, shows that it exists, how?
Hello there! I have been making a game on Mazes. For this I have to destroy a few walls from a grid that I created.Also, I am trying to solve the Maze through Dijkstra Algorithm. For this, I have to check whether the cell(unit of Grid) has wall or not. Unfortunately, the statement when checked for "null" does not compute to true.
In Maze Generator Script
Here the wall are instantiated using : tempWall = Instantiate(wall, newPos, Quaternion.identity) as GameObject;
and are later destroyed using :Destroy(cells[_currentCellNumber].up);
Cells Script(not derived from MonoBehaviour)
[System.Serializable]
public class Cells{
public bool visited = false;
public GameObject up; // 1
public GameObject down;// 2
public GameObject left;// 3
public GameObject right;// 4
}`
Checking in Dijkstra Solve Maze
if (cells[currentCellNumber].left.gameObject == null
Unfortunately, the block of statement for 'If' is never executed. But if the code is changed to: if (cells[currentCellNumber].left.gameObject != null)
The statement does get executed,but for all the wall, also which does not exist. Pleaes help me, thanks in advance!
i think you are destroying Destroy(cells[_currentCellNumber].up); up and you are checking left
whether it exist or not? em i right? you have to check
if (cells[currentCellNumber].up.gameObject == null)
ins$$anonymous$$d off if (cells[currentCellNumber].left.gameObject == null No need to check if (cells[currentCellNumber].up.gameObject == null)
you can simply check if (cells[currentCellNumber].up.gameObject)
I have tried that already. I even tried by checking for mesh renderer(which does not show up on the game scene) and also for collider,but it did not work.
Answer by Garazbolg · Jun 09, 2017 at 04:26 PM
I'm not sure but you it might have to do with your Garbage Collector : When you destroy your wall also set your variable to null, like this :
Destroy(cells[_currentCellNumber].up);
cells[_currentCellNumber].up = null;
And instead of checking the gameObject like you do (in a redundant way), do :
if (cells[currentCellNumber].left== null)