1
\$\begingroup\$

I've made 2 scenes where a room is another scene and the other is the outside of it. But whenever I do something in either scenes, and enter the other, it resets everything I did in the first one. I'm thinking of making the scene running in the background the whole time but how can I do that?

\$\endgroup\$
1
\$\begingroup\$

When adding your scene using SceneManager.LoadScene(scene, mode), pass the LoadSceneMode.Additive mode, to add your scene and keep the old one.

This might require some adjustments to make sure both scenes are placed properly in the world and make the transition smooth. By that I mean, when you add a new scene, you shuold make sure to offset it to a specific location to fit properly in your world. On a unity forum here there are many people asking for that feature, so until something changes, the only way is to do it manually.

Another way is to use the Object.DontDestroyOnLoad(target) on your scene, which will keep it in memory unless you destroy it manually. This will keep your scene when loading a new one. However I don't think this is the best way, as this method is better used for things like the player or the UI which are the same from one scene to another.

Solve lag when loading a new scene

Just keep in mind, every time you load a new scene, there might be some lag as everything is loaded on the same thread. You can also use SceneManager.LoadSceneAsync to load the scene in the background, although once the scene is loaded, every object inside it will call the start method, with might still cause some lag.

One way to solve this, is when you enter an area with a few houses, load the scenes with the houses as well, so that when you walk into a house, you don't have to load anything new, you just have to make the house scene visible.

Quests repeat when a new scene is loaded

Whenever you complete a quest, or progress in one, you should save that information somewhere, so the next time (even after the game closes) the player attempts to start the same quest, they will fail as it's solved already.

To solve that, you can save that information together with the player, you can have an array for example, holding all the started quests, and a number showing the progress on the quests, with -1 showing that the quest is finished.

So now, every time the player attempts to begin a quest, the script responsible for that searches that array for that quest, if it doesn't find it, the quest begins, if its there, depending on the state of the quest, display the right message.

The same can be done when you load a new scene. Suppose that when you enter a specific building, an NPC is there if the quest is not started, but the same NPC is gone while doing the quest. In this case, when the scene is loaded, it searches the same array for the quest, if the quest has been started, don't initialize the NPC.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Another common case you'll see the DontDestroyOnLoad is the network manager for multiplayer games. \$\endgroup\$ – Stephan Sep 18 '18 at 16:23
  • \$\begingroup\$ Tom, can you elaborate on the "placed properly in the world" portion? I suspect that implies that the contents of the scene will be loaded relative to the same origin, so if you had two map chunks at 0,0,0 they'd be on top of each other instead of side by side. \$\endgroup\$ – Stephan Sep 18 '18 at 16:25
  • \$\begingroup\$ cool, add it to your answer so people dont have to play comment miner to find it ;) \$\endgroup\$ – Stephan Sep 18 '18 at 16:43
  • 1
    \$\begingroup\$ @Stephan Done :) \$\endgroup\$ – TomTsagk Sep 19 '18 at 8:47
  • \$\begingroup\$ @TomTsagk Hi! Thank you for your answer! while this works and solved a lot of problems, I noticed that it slows the game every time you go back from scene to scene. And although it doesn't reset like it used to (i.e. while doing quests it resets the trigger for quests) it still spawns the objects for that certain quest perhaps it is a problem from a script? \$\endgroup\$ – Nicole James Cresencio Sep 20 '18 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.