Hello, in my game I have a main map with 5 buttons. When the user clicks a button it loads a level, or new scene. Since every level uses the same game objects I just have a button load the same scene and just swap out graphics.
But I am having all kinds of odd issues when loading the same scene with different graphics. My guess is that it is not fully unloading the scene first before loading it fresh again. When the current level ends the user presses a button to return to the map menu where they select another level. This is the code that I use for the button to return to the map menu.
public void loadMainManu(){
SceneManager.LoadScene(1);
SceneManager.UnloadSceneAsync(2);
}
Some of the odd things are like the player health is not return to a value of 100. Or it is 100 and the user gets hit once and dies instantly. It is acting as if it has not been reset or unloaded correctly before reloading. If I quit out of the game and load it again it works fine but that is not how the game should be played.
Hi and thanks, I have been reading over the docs and testing different things. I’m pretty sure unity is not unloading from memory the last level loaded. It’s interesting because when the level is reloaded it starts at the Start() method. But any variables that I created globally are not reset, which it would be if the script was loaded from scratch.
In my Game Controller script I have a global bool gameOver = false; which is also initialized in the global area. When a one of the 2 players dies they change the gameOver bool to true. When the game is reloaded, this variable is already set to true, which should be false. I suppose I can fix this by having all my global variables initialized in the start menu, but it is kind of proof that it is not unloading and destroying all the gameobjects from memory.
Sorry for the late reply, I had real work to do and now I am back to the game. The reason for unloading the game was to reset all the variables. Im testing the game I discovered something interesting. When the the scene re loads it runs the Awake(), Start() and so on. But any global values that I assigned a value in the global area was not rest. for instance I had a bool gameOver = false; and when the one player would win it was set that to true. The scene would unload and then reload. All the variables that I set the values in the Awake() method would be reset, but the global ones would not, if I set the values in the global area.
So I am solving this but making sure I set all the variable values in the Awake() method. I assumed that if you unload a scene from memory it would have to reload the whole scene and instantiate the global variables again but from my testing that is not the case.
Interesting. I never even considered that question. But the values are located on my GameController that is in my Hierarchy. I have the script attached to an empty gameObject that never gets a destroy call on it. It hasn’t needed one because it just runs the game.
So last night I added a Destroy() to my gameController GO that had the script when a new scene was loaded. When this scene was loaded again all the values were reset. So it seems that when you load a new scene in to the player it does not destroy the old scene. It would be nice if Unity documents said that even though you load a new scene your old scene might hang around in memory and cause you problems when you try and reload it again later. using the SceneManager.UnloadSceneAsync(2); did not destroy it from memory. It also fixed another issue I was having with the Awake() and Start() methods not being called, now they are after destroying it.
This would be nice in the future, Unity. SceneManager.DestroyScene(int index); To make sure everything is released from memory.
Unity destroys the old scene completely. The confusion may be over what counts as the old scene – it’s only the gameObjects. For normal classes you created, maybe a big List, gameObjects in the old scene probably had the only links to them, which means they’re garbage collected and destroyed.
But if a gameObject in the scene creates a List and hands a link to a persistent game manager, Unity purposely won’t destroy that List. It would be really hard, for one thing, but you also pretty much told Unity it was outside of the scene. If you need game manager data flushed out on a scene change, that’s always something you need to do. If you really want, make it a monobehaviour, for no reason except to attach it to an empty, making it die with the scene.
Yes, like Tranos said above with his destroy question. Once he said that I understood more about whats going on. So now by destroying the GO that has my GameController script on it, it reloads it just fine when we come back to the map menu scene.
I just learned this the hard way, but make sure you are managing any static class properties you may be using. I was using a GameController script to manage static lists of objects, and Unity does not re-initialize these when you reset the scene.