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

Keep Scene State!

Discussion in '2D' started by zerodragonheart, Jun 13, 2016.

  1. zerodragonheart

    zerodragonheart

    Joined:
    Aug 14, 2015
    Posts:
    5
    Hi! im completly new at this, but im making a huge progress.
    The thing is, i dont have a clue of how to maintain a scene state, i mean, if the player goes back to the previous scene, it will be exactly as if he never been there (all pick ups, enemys and destroyed objects will be there again).

    HOW can i avoid this to happen?

    TY and sorry for my bad english.
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    You are responsible for saving all the positions and reloading them etc.
     
  3. zerodragonheart

    zerodragonheart

    Joined:
    Aug 14, 2015
    Posts:
    5
    Nice, but how can i do that? do you have a xample script?
     
  4. DanielFF

    DanielFF

    Joined:
    Aug 29, 2012
    Posts:
    41
    Hi,

    You can save the infos in disk or memory, it depends on what you need.

    To save on disk you can use PlayerPrefs, (see the docs)

    To save memory, you can use DontDestroyOnLoad in a manager object, or you can use singleton (an object with a static reference to an instance of the class)

    Or even a combination of these solutions.

    My English is bad too :)
     
    Landerk likes this.
  5. zerodragonheart

    zerodragonheart

    Joined:
    Aug 14, 2015
    Posts:
    5
    i've tried the "DontDestroyOnLoad", attached that script to the object, but when you come back to the scene, the object is still there (if you pick up a coin, for example, that object gets DESTROY.OBJECT. when you come back to the scene that coin is there,again)
     
  6. DanielFF

    DanielFF

    Joined:
    Aug 29, 2012
    Posts:
    41
    When a scene is loaded, each object that is defined in the hierarchy will be recreated. If you want a different behavior you need to manage the creation dynamically for each situation. Maybe if you describe your problem more specific way, we can help more.
     
  7. zerodragonheart

    zerodragonheart

    Joined:
    Aug 14, 2015
    Posts:
    5
    I want SOME of the objects (the ones you destroy) to NOT be created again when you load that scene again
    if you destroyed a box on scene 1 and then move to scene 2, when you come back to scene 1, that box SHOULD stay destroyed, and NOT reapear.