Search Unity

PlayerPrefs problems

Discussion in 'Scripting' started by Hapciupalit, May 19, 2015.

  1. Hapciupalit

    Hapciupalit

    Joined:
    Apr 24, 2015
    Posts:
    103
    So I've been struggling with some script for a long time and I can't figure it out
    I have 2 scripts on diferent scenes the first one in the menu :

    Code (JavaScript):
    1.  
    2. var EasyLevelReached_1 : int = 0;
    3. EasyLevelReached_1 = PlayerPrefs.GetInt("SavedEasyLevel1");
    4. function showUnlocked    () {
    5.     if(EasyLevelReached_1 == 1){
    6.         print("Level 1 was finished");
    7.     }  
    8. }
    9.  
    10. and the second one in the first level
    11. function OnTriggerEnter (){
    12.  
    13.         PlayerPrefs.SetInt("SavedEasyLevel1", 1);
    14.         Debug.Log("SavedLevel = 1");
    15.      
    16.         yield WaitForSeconds(2.0); // wait for 5
    17.         Application.LoadLevel("Menu");
    18. }
    19.  
    I can't figure it out why I can't save the SavedEasyLevel1 so after I go back into the menu my SavedEasyLevel1 it's still 0 not 1
     
    Last edited: May 27, 2015
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Code tags, please!
     
    NomadKing likes this.
  3. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    This. Code formatting and readability goes out the window without code tags. Help us to help you! :)

    As for the problem, you're missing a call to Save to actually commit the things you're trying to save.
     
  4. Hapciupalit

    Hapciupalit

    Joined:
    Apr 24, 2015
    Posts:
    103
    So i put the save and still not working:
    Code (JavaScript):
    1.  
    2. function OnTriggerEnter (){
    3.    
    4.         PlayerPrefs.SetInt("SavedEasyLevel1", 1);
    5.         Debug.Log("SavedLevel = 1");
    6.        
    7.         yield WaitForSeconds(2.0); // wait for 5
    8.         Application.LoadLevel("Menu");
    9.        
    10.         PlayerPrefs.Save();
    11.    
    12. }
    13.  
    When I call showUnlocked() the EasyLevelReached_1 is still 0.
     
  5. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    I can't test anything right now, but try moving the GetInt call inside the showUnlocked function.
     
  6. Hapciupalit

    Hapciupalit

    Joined:
    Apr 24, 2015
    Posts:
    103
    It worked ! Thanks alot man. You are a life saver.
     
    NomadKing likes this.