How to Reload a Scene but keeping others already Loaded Scenes?,How to Reaload a Scene and keep others Loaded Scenes without unload then?
Hi...
My game level is composed of many scenes. So, if my player dies, I just reload that scene he was and not all the level, because he can go backwards if he want. But there is the problem... I go playing and loading the scenes. But when I die and reload that scene I was, the other scenes just unload.
If someone could help me... Thank you, and sorry my bad english.,
Answer by TBruce · Sep 25, 2016 at 08:33 PM
When loading a scene do it like this
SceneManager.LoadScene(SceneToLoad, LoadSceneMode.Additive);
I'm already doing it... But think with me... I'm used it 3 times, so I have 3 Loaded Scenes... If I die, I want to load the scene that I was, but keep intact the other 2 scenes, understand? Thank you for your attention @$$anonymous$$avina
What do you mean but keep intact the other 2 scenes?
Also, you may want to do more than just the LoadScene call. for example
void LoadScene(string SceneToLoad)
{
Scene scene = Scene$$anonymous$$anager.GetSceneByName(SceneToLoad);
if ((scene != null) && (!scene.isLoaded))
{
Scene$$anonymous$$anager.LoadScene(SceneToLoad, LoadScene$$anonymous$$ode.Additive);
}
}
That is your game data. You need to come up with a system to save your game data for each scene. When a scene loads you will load the game data. The game data can be saved a different intervals during a game.
The class that allows you to do that is called PlayerPefs. It also allows you to set defaults (which works great when first loading a scene and the values do not exist yet.
For example take this class Scene$$anonymous$$anager, it also requires this library. Add this class to a game object in each scene. Set the appropriate variables in the inspector. These are the functions in Scene$$anonymous$$anager
PuzzleSolved()
EnemyDie()
OnApplicationQuit()
LoadSceneData()
SaveEnemyData()
SavePuzzleData()
SaveSceneData()
Hmmm, what a perfect answer @$$anonymous$$avina Now I see what I'll need to do to have it working like I want. I'll always have to load my scenes, but with a game data to control how it will be loaded (With enemys dead or not, puzzles done or not, this stuffs...) Thank you for your help and your time!
I am sorry I did not have a chance to test the code I gave you last night and found it was riddled with errors, though I do see that you got the gist of it.
But I went and fixed the errors and here is the class. It has been tested and works out of the box, all you need to do is set the appropriate variables in the inspector.
I had to change the name Scene$$anonymous$$anager
to Scene_$$anonymous$$anager
because Scene$$anonymous$$anager
is a Unity class. I also added the randomization of the prefab position and rotation on instantiation.
Yes, I really got the gist of it... For sure this code will help me a loooot!
Thank you one more time.
Answer by FranSabba · Feb 21, 2021 at 12:01 AM
Same issue, just Load the same scene doesn't work, you need to unload previous scene or you will have 2 of the same scenes loaded. The problem is that I can't unload an scene with the same name and index...
Your answer
Follow this Question
Related Questions
Load Scene in Background and Load When a Button is Clicked 0 Answers
Correct use of Application.UnloadLevel ? 1 Answer
Simple - Save and Loading Scene 0 Answers
Is true async scene loading possible? 0 Answers
Async Level loading issue. 0 Answers