- Home /
Restarting a Scene
This is the desired flow for my game: you start in the menu and press space to go to the game, you press space in the game scene to play, you press space when a player has fallen to get back to the menu, you press space again in the menu to go to the game scene again, you press space to play in the game scene BUT I now get an error when setting everything to 'enabled'
MissingReferenceException: The object of type 'Player2' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. Player.gameStart () (at Assets/Players/Player.cs:45) GameManager.triggerGameStart () (at Assets/GameManager.cs:16) GameManager.Update () (at Assets/GameManager.cs:31)
This is the code in the Game scene that causes the transition to the menu. The first space bar condition is the one that starts the game if you're at the beginning waiting to play, and the second condition is for getting back to the menu after a player dies.
void Update(){
if (Input.GetKeyUp ("space") && !isRunning&&!hasRun) {
// print ("space was hit");
triggerGameStart ();
isRunning = true;
}
if (Input.GetKeyUp ("space") && !isRunning && hasRun) {
print ("hit space");
StartCoroutine(swapScene ());
}
}
IEnumerator swapScene(){
print ("called method");
yield return new WaitForEndOfFrame();
Application.LoadLevel("MenuScene");
}
Before it threw an error about player two, it was complaining about a piece of text. I was able to fix the text problem like this (in text manager):
void gameStart(){
if(startInfo!=null)
startInfo.enabled = false;
}
So it moved on to finding a problem with player. I don't know why things are being considered "destroyed" because my code does not destroy these things, merely disables them and enables them depending on if the action is currently on. When I transition from the menu to the game for the second time, I want it to pretty much start up all the GameScene scripts as if for the first time, because the game scene runs just fine the first time. What is happening differently when I load the level again?
Answer by getyour411 · Jan 29, 2014 at 07:22 AM
As you move from scene to scene, things that dont have DontDestroyOnLoad are wiped. I think this is what you are missing
http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html
Well for this I actually want everything to be wiped. When I transition from game scene to menu scene after somebody dies, I want all the game stuff gone to be set up anew once I transition from menu to game once again. So as it stands right now, everything is getting destroyed from game scene when a player dies as it takes me back to the menu. When I reload the game scene, shouldn't it put everything back in its original position, run the Start methods again, etc.? I just want it to launch the game scene as if it's the first time, every time.
Your answer
![](https://koobas.hobune.stream/wayback/20220613131918im_/https://answers.unity.com/themes/thub/images/avi.jpg)