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

Pause a scene, load a new one, then resume first scene

Discussion in 'Scripting' started by Scilleen, Apr 3, 2015.

  1. Scilleen

    Scilleen

    Joined:
    Oct 3, 2013
    Posts:
    4
    Hey guys,

    I have been working on my first unity game for about a month now. I am making an action RPG and I have made two levels that work great. Now I just need to connect them. The second scene is a cave that can be entered through the first scene. I want the player to be able to enter/leave the cave freely and the scenes remain the same as they were left.

    I have spent hours watching videos and searching up how to do this, but no luck. I figured it would be just as easy as pausing the scene, but apparently not. I know I could use PlayerPrefs to save the players health and experience and such, but the tricky part is each level have around 25 enemies. (I have 3 different prefabs for different enemies)

    So if I kill Goblin 3 in scene one, go into the cave, and return, I want Goblin 3 to still be dead. With each scene having about 12 goblins, you can see why using PlayerPrefs won't do it.

    Any tips on how to approach this? I have been looking for a solution for a few days now and would love to figure it out so I can continue making new levels!

    Thanks in advance.
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,533
    This is actually fairly complicated. It's similar to saving a game. Before leaving the first scene, you need to record the state of important objects and store it somewhere. When returning, you need to retrieve and reapply that state. The good news is that once you have it working, the hard part of your saved game system is also done.

    Some people take a brute force approach and serialize the entire scene, which captures the state of everything in it. When the player returns to the scene, they deserialize it.

    Others take a more targeted approach and record only specific data on specific objects.

    You might find Mike Talbot's Unity Serializer handy.

    I'll describe how the Dialogue System's save system works, since I'm pretty familiar with it. Level designers add "persistent data" scripts to GameObjects. These scripts implement Record() and Apply() methods. Record() stores the current state of certain GameObject properties in persistent storage. Apply() retrieves the state from persistent storage and applies it back to the GameObject.

    Before changing levels, a manager script calls Record() on all GameObjects in the scene. So rather than calling Application.LoadLevel(), I'd call LevelManager.LoadLevel(), which calls Record() before calling Application.LoadLevel().

    After changing into the new level (e.g., the cave), the manager calls Apply(). This tells the new level to retrieve its state from persistent storage. You can hook into Application.OnLevelWasLoaded() to know when new levels are loaded.

    When you return to the first level, it once again calls Apply(), which tells the first level to retrieve its state from persistent storage.

    Saving a game is as easy as calling Record(), then saving the persistent storage somewhere like PlayerPrefs or a local disk file.
     
  3. Scilleen

    Scilleen

    Joined:
    Oct 3, 2013
    Posts:
    4
    Thanks for the reply.

    I will try these things out