Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

The method SceneManager.UnloadScene return true, but the scene prefabs stay actives

Discussion in 'Scripting' started by bart_bender, Oct 14, 2016.

  1. bart_bender

    bart_bender

    Joined:
    Mar 21, 2015
    Posts:
    20
    Hello,
    I have the next method for unload an additive Scene Loaded with a GUI menu to continue the game if you die.
    The code is allocated in a singleton gamemanager instance (with DonDestroyOnLoad applied)

    Code (CSharp):
    1.  internal void ContinueGame()
    2.   {  
    3.   if (SceneManager.UnloadScene("RealiveMenu"))
    4.   {
    5.   _aSource.PlayOneShot(SoundContinue);
    6.   settings.balasPlata -= BalasToContinue;
    7.   switch (Instance.BalasToContinue)
    8.   {
    9.   case 1:
    10.   BalasToContinue = 2;
    11.   break;
    12.   case 2:
    13.   BalasToContinue = 5;
    14.   break;
    15.   case 5:
    16.   BalasToContinue = 10;
    17.   break;
    18.   case 10:
    19.   BalasToContinue = 20;
    20.   break;
    21.   case 20:
    22.   BalasToContinue = 50;
    23.   break;
    24.   default:
    25.   BalasToContinue = 50;
    26.   break;
    27.   }
    28.   _aSourceMainCamera.Stop();
    29.   _aSourceMainCamera.clip = musicGamePlay;
    30.   _aSourceMainCamera.Play();
    31.  
    32.   GameManagerCanvasActive(true);
    33.   fxCamera.SetActive(true);
    34.   prefabAgua.SetActive(false);
    35.   prefabIman.SetActive(false);
    36.   _pbCruz.gameObject.SetActive(false);
    37.   _textCruz.gameObject.SetActive(true);
    38.   _cruzActiva = false;
    39.   _lifes = 2;
    40.   enableFX = false;
    41.   GameStarted = true;
    42.   playerController.Reanimate(true);
    43.   }
    44.    
    45.   }
    Sometimes the code works well, but sometimes the scene not is unloaded and the method return true.

    Can you help me with it?

    Thanks in advance
    Best regards
     
  2. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    From documentation of UnloadScene:

    Sometimes there might be also certain "lock" scenarios, where some reference is keeping things in memory, even if all GameObjects are supposed to be destroyed.

    As a sidenote, if you're already using additive scenes, I'd recommend getting rid of the DontDestroyOnLoad and put your scene manager scripts in a separate scene that you just dont unload during playtime, because (from DontDestroyOnLoad manual):
    And from multiscene editing docs:
    So in short I'd start with changing to UnloadSceneAsync, then get rid of DontDestroyOnLoad. This will give you finer control of what is happening and should give more consistent results. In a sense you could (almost) treat additive scenes like threads - while they can work together for great results, it's much easier to make them misbehave or "interlock" (in a loose meaning of the word).
     
  3. bart_bender

    bart_bender

    Joined:
    Mar 21, 2015
    Posts:
    20
    Thanks for your help, i will try it.

    Sorry for my english