0

I have a problem with the ScenManager of my unity android app. When I start a new Scene the old scene is activ and work but the new scene is shown an work. I want that only the new scene work.

// here I load the new Scene
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);

I tried a lot but in every I found the old scene gets atomatically destroyed.

julianpjp
  • 112
  • 1
  • 8
  • 1
    Its unclear what you're asking. You say "I want that only the new scene work", then you say "I tried a lot but in every I found the old scene gets atomatically destroyed", so its unclear whether you want the old scene around or not. – TheBatman Feb 07 '20 at 16:49
  • I am sorry my Englisch is not the best. In everything I found the old scene not work when you start the new scene but in my case the old scene is working even though i started the new scene and i dont know why – julianpjp Feb 07 '20 at 16:54
  • Do you mean that objects in the "old scene" are still around in the newly loaded scene? I'm having a hard time understanding how two scenes could be active at the same time. – TheBatman Feb 07 '20 at 16:56
  • The advantage of having many scenes is you can actually have your UI etc as a scene, so its the same thing through out your whole game – BugFinder Feb 07 '20 at 19:35

2 Answers2

0

I agree with derHugo. You do not need to manually unload scenes if you use LoadSceneMode.Single.

See this documentation related to OnDestroy as well:

"Destroying the attached Behaviour will result in the game or Scene receiving OnDestroy.

OnDestroy occurs when a Scene or game ends. If a Scene is closed and a new Scene is loaded the OnDestroy call will be made.

When built as a standalone application OnDestroy calls are made when Scenes end. A Scene ending typically means a new Scene is loaded."

https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDestroy.html

In terms of solving your problem, use LoadSceneMode.Single. In you want to keep the previous scene active, use LoadSceneMode.Additive.

-1

Scenes have to be unloaded manually, otherwise they will stay active. Check out the SceneManager documentation.

In your case, unloading the old scene using
SceneManager.UnloadSceneAsync(sceneName);
should work.

You can also try to experiment with the mode parameter. But as Single is the default value it properbly does not what you want:
SceneManager.LoadScene(sceneName, LoadSceneMode.Single);

aalmigthy
  • 1,261
  • 8
  • 19
  • 2
    The default mode is `LoadSceneMode.Single` anyway. Scenes do **not** have to be actively unloaded when using `LoadScene `without the mode parameter (equals passing in `Single`) – derHugo Feb 08 '20 at 08:22