Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jonathanrune2015 · Jul 19, 2018 at 08:58 PM · build settingssave scenesave game

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.
 }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jonathanrune2015 · Jul 19, 2018 at 10:48 PM 0
Share

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.

avatar image jonathanrune2015 · Jul 19, 2018 at 11:22 PM 0
Share

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?

avatar image jonathanrune2015 · Jul 19, 2018 at 11:35 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

87 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to make cloud based game saving state, 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges