- Home /
Scene Saving In Between Runtimes
Hi, I'm making a 3D horror-ish game that sometimes forces the game to exit. And because of that, I need a way to save which scene the player is in so that when the next time the game is started back up again. It will load into the scene where the player left of.
I myself have no clue how to do this, I have searched videos and google and I simply can't find a thing on the subject.
Thanks, already.
Answer by Cynikal · Jul 19, 2018 at 09:13 PM
You can save the scene in the PlayerPrefs, and then when they load the game again, just check what scene they were on.
https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
So,
void OnForceExit()
{
PlayerPrefs.SetInt("ForceQuitLevel", SceneManager.GetActiveScene().buildIndex);
Application.Quit();
}
void OnGameStartupOrWhatever()
{
if (PlayerPrefs.GetInt("ForceQuitLevel") != null)
{
//load the NEXT scene index here.
}
}
Two things... does the load and save lines have to inside voids? And when you mention "load next scene here" does that mean I have to literally have to write
Scene$$anonymous$$anager.LoadScene("SecondRoom");
(Note the force quit was in the "FirstRoom")
Edit: Here is how I thought I had to implement it.
(We are in "FirstRoom" and the collider also quits the game on another script)
public class Save : $$anonymous$$onoBehaviour {
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
PlayerPrefs.SetInt("ForceQuitLevel", Scene$$anonymous$$anager.GetActiveScene().buildIndex);
}
}
}
(Now we are in the menu trying to load the scene after a force quit);
public class GameStart : $$anonymous$$onoBehaviour {
public GameObject blackFade;
void Start()
{
blackFade.SetActive(false);
}
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
StartCoroutine("changeScene");
blackFade.SetActive(true);
} else
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape))
{
Application.Quit();
} else
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return))
{
if (PlayerPrefs.GetInt("ForceQuitLevel") != null)
{
Scene$$anonymous$$anager.LoadScene("SecondRoom");
}
}
}
IEnumerator changeScene()
{
yield return new WaitForSeconds(1f);
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1);
}
}
(Note; There is three things to do in the menu quit (quits the game), new game (places you in "FirstRoom"), and continue which well, loads i guess?. (It always loads the second room no matter if i have even gone thru the trigger in the "FirstRoom" or not.
Alright, scratch that! I found another method that seems to work with the whole saving CURRENT scene. But what if I wanna save the next scene in line?
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
PlayerPrefs.SetInt("Level", Scene$$anonymous$$anager.GetActiveScene().buildIndex);
PlayerPrefs.Save();
}
}
}
Im guessing i somehow have to change GetActiveScene to something else?
I found out. I just had to add "+ 1" at the end of the buildIndex line [Working Code] (Save)
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
PlayerPrefs.SetInt("Level", Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1);
PlayerPrefs.Save();
}
}
}
[Load]
void Start()
{
blackFade.SetActive(false);
}
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
StartCoroutine("changeScene");
blackFade.SetActive(true);
} else
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape))
{
Application.Quit();
} else
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return))
{
Scene$$anonymous$$anager.LoadScene(PlayerPrefs.GetInt("Level"));
}
}
IEnumerator changeScene()
{
yield return new WaitForSeconds(1f);
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1);
}
}
Your answer
Follow this Question
Related Questions
How to Save and Load a Level by Serialization 3 Answers
What can i do when my built Scene differs from my scene in the Editor? 0 Answers
Save Game Sceenes user quit aplication, Can I save the scene as a whole while leaving the game? 0 Answers
How do you save an instance of a game object in Unity C# after exiting the game? 1 Answer