Destroying a cloned gameobject when a new scene is loaded
Hey! I am building upon two of Unity's Live-Session tutorials: 2D Roguelike and Basic 2D Dungeon Generation. The tutorial uses a scene manager to reset the level when the player reaches an objective tile.
All tiles of the tilemap are held in a GameObject called boardHolder. It acts as a container to hold all tiles to prevent the Hierarchy from becoming too long.
Once a new scene is created and a new layout generated, a new boardHolder is created.
However, in my tests, when a new scene is created, the old boardHolder still exists and causes conflicting layouts.
My question is it possible to destroy a previously instantiated gameobject during the creation of the new scene?
I've tried things like checking:
if (boardHolder != null)
this.Destroy(boardHolder);
// Steps
boardHolder = new GameObject("BoardHolder");
Or setting boardHolder to null.
Here is a screenshot of how the hierarchy looks like:
I greatly appreciate any help you guys can offer.
I forgot to mention. if (boardHolder != null) this.Destroy(boardHolder);
// Steps
boardHolder = new GameObject("BoardHolder");
Is performed in a public method named SetupScene() that is called when generation is needed.
Answer by jim3878 · May 31, 2018 at 02:34 AM
i thought that some code set your boardHolder to null.
try this
Destroy(GameObject.Find("BoardHolder"));