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. Dismiss Notice

DontDestroyOnLoad problem when going back and forth between 2 scenes

Discussion in 'iOS and tvOS' started by ciaravoh, Sep 18, 2011.

  1. ciaravoh

    ciaravoh

    Joined:
    Dec 16, 2009
    Posts:
    252
    Hello guys

    First I want to mention that I've already search the forum about that issue and found nothing.

    I'm using a first scene where the player choose the level of the game. then a start game to load the level.

    At a certain stage, the player can choose to go back to the first scene to change the difficulty for example.

    Each object with the DontDestroyOnLoad will then get duplicated and a new instance will be created each time I switch between the 2 scenes.

    I use this instruction for my Static class (all the setup is stored there) and I also use some plugin from Prime31 (GameCenter and SocialNetwork) : same issue there. All the GameObjects are duplicated.

    Any idea that could help me there ?

    thank you.
     
  2. ciaravoh

    ciaravoh

    Joined:
    Dec 16, 2009
    Posts:
    252
    Plus one ?
     
  3. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,039
  4. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
  5. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    Can you make a loading scene with all the global stuff and then auto-load the menu scene? That way the menu scene doesn't have any of the global stuff in it.
     
  6. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    The best way is always to keep don'tdestroy stuff on a scene that is never called again in this case (what rafes said).
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    I personally would recommend going with singletons too. Not just cause its cleaner but also cause they are significantly more powerfull in usage and bundling of relevant functionality and data management.

    If you are new to patterns etc, the head first patterns book is a great way to learn about them
     
  8. ciaravoh

    ciaravoh

    Joined:
    Dec 16, 2009
    Posts:
    252
    Thank you all.

    many problems with Singleton.

    1 - A long music will start with the welcome screen. I don't want the music to be stopped when the player starts to play. I want the music to be continuous.
    2 - some plugins (Prime31 social and GC for example) use this DontdestroyOnLoad feature. I can't place these plugins in the first scene, because the player can and will go back to this scene each time He wants to restart his game or when He has finished the round.

    I have merged all the scene into one. Thanks to the UIToolkit, it has been a smooth process.

    Thank you for all your advice.
     
  9. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    I don't think you understood our advice. Don't let the player go back to the first scene. Make that the load scene and have nothing but persistent stuff in there. Load the first scene, then auto-load the menu scene (the one the player can come back to). This happens instantly and the player won't know about it.
     
  10. ciaravoh

    ciaravoh

    Joined:
    Dec 16, 2009
    Posts:
    252
    Rafes,

    Thank you for the advice. I did understood and what I did is ok because the different difficulties of my game will take place in the same scene. I will have only 2 "screens" in my game.

    The merge is now finished and really it works fine this way.

    Next one I will build the game the way you adviced me.

    thank you.

    Hervé
     
  11. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    Ah, OK. I just wanted to make sure you received all the info. If what you have works then that is great!
     
  12. thinkerton5

    thinkerton5

    Joined:
    Apr 16, 2014
    Posts:
    5
    Years old post just saved my sanity. This is way easier than the singleton implementation. :cool:
     
  13. PerfectlyInsane

    PerfectlyInsane

    Joined:
    Aug 9, 2013
    Posts:
    74
    Yep simpler than singleton. cheers :)
     
  14. tmkirankumar

    tmkirankumar

    Joined:
    May 24, 2023
    Posts:
    4
    I have two Scenes with prefab authentication in Scene 1 and game play in Scene 2, I want to go back to Scene 1 and active the one game object.
    When i am trying to use dont destroy scene concept. It is working with reloading the compelte Scene 1, and reloading the prefab authentication part.
    How to skip the prefab authentication and go directly to game object of scene 1 from scene 2 ?