- Home /
World hierarchy doesn't finish destroying before starting a new world?
Hi all
This is an awkward one to explain concisely.
Basically, I have a "restart the game" function that looks like this:
function startNewGame(){
// If there is a game in progress, end it
var currentGame = gameObject.GetComponent(gameScript);
Destroy(currentGame);
// Start a new game
gameObject.AddComponent(gameScript);
}
gameScript's Destroy function goes on to Destroy() the world, the players, etc and those in turn Destroy() their own children.
Now, I think what I'm seeing is this: the game hasn't finished clearing itself up before the new game is started. I'm getting artefacts like players falling through the level on the 2+ time the level is created, because it would seem the first and subsequent games haven't been destroyed properly.
One thing I have tried is making it so after the first game everything is destroyed but the second level is not created. In the inspector I then trigger the creation of the next level. This is made flawlessly!
I'm really at a loss of what to do now. Is my method of cleaning up the game completely wrong? How can I ensure everything is destroyed before moving on to build the next level?
Any advice is massively appreciated.
Thanks,
Vesuvian
Why don't you use a simple Application.LoadLevel? It destroys everything in the level and creates all objects of the new level. If you want to reload a level, use Applcation.LoadLevel(Application.loadedLevel)
Answer by rutter · May 16, 2012 at 05:22 AM
You might want to check out the official script reference page for Destroy(), which I will quote in part:
Actual object destruction is always delayed until after the current Update loop
Like aldonaletto says, you might try just loading (or reloading) a level.
If you really want to do things this way, you could do something that waits one frame, or otherwise makes sure that the object(s) in question have been destroyed.
You might try DestroyImmediate(), but the script reference doesn't recommend it.
Thank you very much, I can't believe how often I read the Destroy reference page without that sinking in.
Don't use DestroyImmediate. It's mainly ment for editorscripts. You can't use Destroy in the editor since there are no "frames" in the editor the Destroy could wait for.
Loading levels is the simplest way. Another one is to use a coroutine and wait 1 frame ;)
function startNewGame(){
var currentGame = gameObject.GetComponent(gameScript);
Destroy(currentGame);
yield;
gameObject.AddComponent(gameScript);
}
Just as a hint: Class names usually should start with a capital letter so they aren't mistaken with variable names. It's a basic convention which is used by almost all program$$anonymous$$g styles out there. Sure, it doesn't matter when you're the only one that works on this project, but at least now we are involved as well ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613071530im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
getting udp package info inside unity (GlovePIE) 0 Answers
Destroy(gameObject) fails once in a while 2 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Problem using 'function Update ()' to restart the game, error CS0246 2 Answers
2D C# destroy a GameObject on collision 2 Answers