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 /
  • Help Room /
avatar image
0
Question by WilliamGLC · Mar 10, 2018 at 08:44 PM · playerprefssavingsave data

Problem saving with PlayerPrefs

Hey!

So, what I have is a PlayerPrefs.SetInt at the end of my level one (as well in a shortcut in this same level) wich sets a Static Int to 2 (to load the level 2). It's working fine and everything. But if someone doesn't ends the firts level and quits (quits by the main menu), the level saved in the Int is the 2nd wich is a nonsense since (I think) i have set it property to avoid this kind of issue.

I post the code for my MainMenu so you can try to tell me.

 public class MainMenuButtons : MonoBehaviour {
  
     [Header ("UI")]
     public GameObject uiMainMenu;
     public GameObject uiOptions;
     public GameObject uiExit;
     public GameObject loadingScreen;
  
     [Header ("Audio")]
     public    AudioSource    Wind;
     public    GameObject     SFXFireCamp1;
  
     [Header ("Others")]
     public         Slider            sliderBar;
     public         Text             progressText;
  
  
     const int LEVEL_01 = 1;
     const int LEVEL_02 = 2;
     const int LEVEL_03 = 3;
  
     public static int levelToOpen = 1;
  
     void Awake () {
     }
  
     void Start () {
         levelToOpen = PlayerPrefs.GetInt ("LevelToLoad");
         print (PlayerPrefs.GetInt ("LevelToLoad"));
         if (levelToOpen == LEVEL_01) {
             Debug.LogWarning ("S'obrira el Level_01!!");
         }
         if (levelToOpen == LEVEL_02) {
             Debug.LogWarning ("S'obrira el Level_02!!");
         }
         if (levelToOpen == LEVEL_03) {
             Debug.LogWarning ("S'obrira el Level_03!!");
         }
  
         Time.timeScale = 1;
  
         Cursor.lockState = CursorLockMode.Confined;
  
         SFXFireCamp1 = GameObject.Find ("PS_CartoonFlames01");
  
         uiMainMenu.SetActive    (true);
         uiOptions.SetActive     (false);
         uiExit.SetActive         (false);
     }
  
     void Update () {
         Time.timeScale = 1;
         ExitPopUp ();
  
      
  
         if (Input.GetKeyDown (KeyCode.R)) {
             Debug.LogWarning ("Resetejat, s'obrira el Level_01");
             levelToOpen = LEVEL_01;
         }
     }
  
     public void ButtonPlay (int sceneIndex) {
         if (levelToOpen == LEVEL_01) {
             StartCoroutine (LoadAsyncProgress (sceneIndex));
         }
         if (levelToOpen == LEVEL_02) {
             StartCoroutine (LoadAsyncProgress2 (sceneIndex));
         }
         if (levelToOpen == LEVEL_03) {
             StartCoroutine (LoadAsyncProgress3 (sceneIndex));
         }
     }
  
     IEnumerator LoadAsyncProgress (int sceneIndex) {
         AsyncOperation operation = SceneManager.LoadSceneAsync ("Level_01");
         loadingScreen.SetActive (true);
         while (!operation.isDone) {
             float progress = Mathf.Clamp01 (operation.progress / .9f);
  
             sliderBar.value = progress;
             progressText.text = progress * 100f + "%";
  
             yield return null;
         }
     }
  
     IEnumerator LoadAsyncProgress2 (int sceneIndex) {
         AsyncOperation operation = SceneManager.LoadSceneAsync ("Level_02");
         loadingScreen.SetActive (true);
         while (!operation.isDone) {
             float progress = Mathf.Clamp01 (operation.progress / .9f);
  
             sliderBar.value = progress;
             progressText.text = progress * 100f + "%";
  
             yield return null;
         }
     }
  
     IEnumerator LoadAsyncProgress3 (int sceneIndex) {
         AsyncOperation operation = SceneManager.LoadSceneAsync ("Level_03");
         loadingScreen.SetActive (true);
         while (!operation.isDone) {
             float progress = Mathf.Clamp01 (operation.progress / .9f);
  
             sliderBar.value = progress;
             progressText.text = progress * 100f + "%";
  
             yield return null;
         }
     }
  
     public void ButtonQuit () {
         PlayerPrefs.Save ();
         Debug.LogWarning ("Progres Guardat");
         Debug.Log ("S'obrira el nivell " + levelToOpen);
         print (PlayerPrefs.GetInt ("LevelToLoad"));
         Application.Quit ();
     }
  
     public void ButtonCredits () {
         SceneManager.LoadScene ("Credits");
     }
  
     public void ExitPopUp () {
         if (uiExit.activeSelf == true) {
             Wind.volume = 0.5f;
             SFXFireCamp1.GetComponent <AudioSource> ().volume = 0.5f;
             if (Input.GetKeyDown (KeyCode.Escape)) {
                 uiExit.SetActive (false);
                 uiMainMenu.SetActive (true);
             }
         } else {
             Wind.volume = 1f;
             SFXFireCamp1.GetComponent <AudioSource> ().volume = 1f;
         }
     }
 }


Basically everytime I open the main menu it sets the leveltoopen to 2. I can't find out what it's causing it, so some help would be usefull!

Also, the next line wich is in the ButtonQuit void prints 2 but the debug.log says 1. print (PlayerPrefs.GetInt ("LevelToLoad"));

And also 2: Sorry for my english, not an english speaker! :'D

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

0 Replies

· Add your reply
  • Sort: 

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

130 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 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

PlayerPrefs help ! 1 Answer

Saving In Between Scenes and Program Exit 0 Answers

How do I apply and save my game settings? 0 Answers

PlayerPrefs Not Saving When I Click the Button. 1 Answer

Using PlayerPrefs to remember transform heirarchies? 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