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

Best way to keep a scene as it is when going to another scene

Discussion in 'Scripting' started by Sorade, Feb 1, 2017.

  1. Sorade

    Sorade

    Joined:
    Jan 3, 2017
    Posts:
    8
    Hi all, I am new to Unity and C#, so I hope that the question is in the correct forum and not too trivial.

    I basically starting a game building on the Space Shooter Tutorial. I made a new scene with a UI set of menus in which the player can purchase planets and ships based on his score.

    So far I managed to pass on variables between my scenes using a "storage script" which is accessible but not attached to any object in any scene. It works well to pass the score around and set the booleans associated with the bonuses to true or false. However I did a bit more of an advanced test of the game yesterday and I realised that every time I go back from the Space Shooter Tutorial scene to my menu scene all the changes the player made are reset.

    Before jumping in and adding an endless number of values to my "storage script" which would require setting up every time I quite the menu and recovering every time I go back on it, I was wondering if there was an easier way of going about it ?

    I was thinking maybe using the `DontDestroyOnLoad` function by making all the objects in my menu scene children of an empty gameobject and flipping its status to active/inactive every time I need the menu. This seems to me like an easier way to go about it, expect I might encounter troubles when trying to implement a "Save" functionality to the game.

    At this point I am really open to any suggestion, I have just been able to skim the surface of the possibilities offered by Unity and I'm probably not setting about doing things in the easiest of ways.
     
  2. smacbride

    smacbride

    Joined:
    Jan 10, 2015
    Posts:
    50
    Check out the Adventure Tutorial, they go over this in great detail. It does require some advance programming concepts like object oriented development etc, but very necessary if you every want to get anywhere in Unity.
     
    Sorade likes this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    I would not use dontdestroyonload for this. You'll just be passing stuff around between scenes with the chance of running into errors. Plus when you go back to the game scene, it's going to be reset anyways, which means you'll have duplicates and suddenly you're left dealing with that and just more chances of errors.

    I have not seen the Adventure Tutorial, so I am curious what they use, but the end result is you'll have to save out data that you know will need to be changed and setup based on what you do.
     
  4. Sorade

    Sorade

    Joined:
    Jan 3, 2017
    Posts:
    8
    Thank you for your answers. I will have a look at the adventure tutorial.