reloading a scene after death - resetting scripts
So I have a saved game. when I launch the editor and open my pause menu screen and hit "load saved" everything works as expected. I am where i was and all the playerPrefs have loaded. Now if I play and die, "game over" appears and I get thrown back to the beginning of the level - as I want. Problem is that if I hit "load saved" from the pause menu now - without quitting out of play mode in the editor - I do go where I am suppose to and some of the saved game info works but some doesn't. Bottom line is i have to quit out of PLAY mode in the editor and then go into play mode and load the save level then . I guess it has something to do with the void Start() functions? that when entering PLAY mode they get activated making the "Load Saved" work then, but if I die and go back to the start and hit "Load Saved" it doesn't work because the Start functions aren't restarting? I don't know. DOes this make sense and how can I load a save point without stopping play mode?. I imagine in a build this would amount to having to quit out of the game app and restarting it to load a save point successfully. How do I reset all my scripts after death as well player position?
Answer by Lilius · Feb 23, 2018 at 07:37 PM
Maybe you could reload the scene:
using UnityEngine.SceneManagement;
public void RestartScene()
{
Scene thisScene = SceneManager.GetActiveScene();
SceneManager.LoadScene(thisScene.name);
}
Then load saved game data. You might want not to destroy some of your scripts and objects, use DontDestroyOnLoad: https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Edit: Re-read the question and maybe this is what you are doing already? What happens when you die, do you reload the scene?
thanks....I actually stumbled onto something like this. I have a "restart" button in my pause menu as well - one i never even used until just a few $$anonymous$$utes ago. and after I hit the restart button and was taken back to the start of the level and THEN hit "Load Saved" it worked without having to quit out of play mode and all the code said was
Application.LoadLevel(Application.loadedLevel);
Good. By the way, what version of Unity you are using? You should change that code to the one on my answer, because Application.LoadLevel is obsolete now and will not work if you update Unity.
I am using an ancient version 4.7. The game i am making is just a hobby that has been in the works off and on for a few years . I think I started in version 3. something. I have updated to 4.7 but that is as far as I go while working on this project. Tried not once but TWICE to upgrade to version 5 but it broke too many things - probably because of a lot of scripting code being obsolete - and simply not in the mood to back track and change code. Frankly a bit scared to upgrade even after I am done with this project as learning coding was a challenge and I'd hate to have to relearn the new changes. I have been making some changes just to eli$$anonymous$$ate some of the warnings. Like my graphics menu use to say QualitySettings.currentLevel = QualityLevel.Fantastic; and changed to the new and improved QualitySettings.SetQualityLevel(6);
Your answer
Follow this Question
Related Questions
How do I reset my game but keep the Player in the same position? 1 Answer
I need to fix a problem where my money resets when i continue to the next level 0 Answers
Retstart Key in C# or Javascript 1 Answer
Why is my unity trying to re import my game? 0 Answers
SetActive(false), and then back to true to call OnEnable 0 Answers