- Home /
How to destroy a game object within an instance of prefab
hi, i am new to unity and I would like some help with this problem.
i am trying to make a simple maze generator. I have a prefab called "cell" which has 5 cube objects within it to represent the 4 walls and the floor. I am able to instantiate the prefab to form a 10x10 grid, with each instance having a unique name such as cell_1 , cell_2... to track and access them at runtime with gameobject.find();
to generate the maze ,the algorithm im using will choose a random direction and move forward by 1 cell in that direction if certain conditions are met. (the algorithm itself is not relevant to this question)
the problem im having is with destroying the walls between the two cells to form a path for the maze.
i cant seem to be able to find a way to access the individual wall game objects contained within the instance of the cell prefab. (are these considered children of cell game object?)
am i missing something here or is there another way to achieve this?
thanks in advance.
Answer by Paulius-Liekis · Aug 03, 2013 at 09:27 PM
Something like this?
GameObejct cell = gameobject.find("cell_5");
Transform wall = cell.transform.find("wall_3");
Destroy(wall.gameObject);
this is exactly what I was looking for. works perfectly when integrated into my code. so much better than the work around I was trying to implement by triggering a script component on individual walls with a destroy function.
thanks a lot mate.