Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

PlayerPrefs not working

Discussion in 'Scripting' started by Mid3x, Jul 17, 2020.

  1. Mid3x

    Mid3x

    Joined:
    Jul 12, 2020
    Posts:
    7
    As you saw in the title i think there something wrong with Playerprefs, or I'm bad and a see this as a more probable cause. However i'm makig my first game and i added many levels but all except the first one are locked and you are supposed to unlock them by finishing the level because a added a trigger with a script. In the trigger script I'm changing a int if the player passes trought the trigger and in my select level script I unlock the level2 button only if that int changed. For some reason it doesn't work and i don't know why. I'm pretty entry level with programming and game dev, so pls help me! Btw I'm italian so if you see words that you don't recognise (like "livello" that means "level") that's why. The first code is the trigger one and the second the one i added to my button in the levelselect scene. Thanks for the help!


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4.  
    5. public class EndTrigger1 : MonoBehaviour
    6. {
    7.     int livello1Completed = 0;
    8.     public GameManager gameManager;
    9.  
    10.  
    11.     void OnTriggerEnter ()
    12.     {
    13.      
    14.         PlayerPrefs.SetInt ("Livello1Completed", 1);
    15.         gameManager.CompleteLevel1();
    16.        
    17.     }
    18.      
    19.  
    20. }
    21.  
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using UnityEngine.UI;
    4.  
    5. public class Livello2 : MonoBehaviour
    6. {
    7.     int livello1Completed;
    8.     public float restartDelaydue = 1f;
    9.     public void DelayLivellodue()
    10.     {
    11.         Invoke("Livellodue", restartDelaydue);
    12.     }
    13.     public void Livellodue()
    14.     {
    15.         livello1Completed = PlayerPrefs.GetInt ("Livello1Complete");
    16.         if (livello1Completed == 1)
    17.         SceneManager.LoadScene("Livello2");
    18.  
    19.     }
    20.  
    21. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    It works just fine, but check your spelling. Your SetInt string and your GetInt string don't match.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    I suggest creating a
    const string Lvl1CompletedKey = "Livello1Complete";


    Then use that constant instead of typing the string in each time. If you make a typo you'll get a compiler error instead of your game just mysteriously not working.
     
    MaximumTre likes this.
  4. Mid3x

    Mid3x

    Joined:
    Jul 12, 2020
    Posts:
    7
    Oh, that's right. And i swear i checked everything more then once. Thank you so much guys!
     
  5. Mid3x

    Mid3x

    Joined:
    Jul 12, 2020
    Posts:
    7
    Ok so i tryied but it doesn't work... even if i didn't complete the level, if i press my button for level 2 it loads. Probably i messed something around. However this are the script now, maybe you can see what's wrong.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class EndTrigger1 : MonoBehaviour
    5. {
    6.     int livello1Completed = 0;
    7.     const string Lvl1CompletedKey = "Livello1Complete";
    8.     public GameManager gameManager;
    9.    
    10.     void start(){
    11.         livello1Completed = PlayerPrefs.GetInt (Lvl1CompletedKey);
    12.     }
    13.     void OnTriggerEnter ()
    14.     {
    15.        
    16.         PlayerPrefs.SetInt (Lvl1CompletedKey, 1);
    17.         gameManager.CompleteLevel1();
    18.        
    19.     }
    20.        
    21.    
    22. }
    23.  
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using UnityEngine.UI;
    4.  
    5. public class Livello2 : MonoBehaviour
    6. {
    7.     int livello1Completed;
    8.     const string Lvl1CompletedKey = "Livello1Complete";
    9.     public float restartDelaydue = 1f;
    10.     public void DelayLivellodue()
    11.     {
    12.         Invoke("Livellodue", restartDelaydue);
    13.     }
    14.     public void Livellodue()
    15.     {
    16.         livello1Completed = PlayerPrefs.GetInt (Lvl1CompletedKey);
    17.         if (livello1Completed == 1)
    18.         SceneManager.LoadScene("Livello2");
    19.    
    20.     }
    21.  
    22. }
    23.  
     
  6. Mid3x

    Mid3x

    Joined:
    Jul 12, 2020
    Posts:
    7
    Does anyone have any idea?
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
  8. Mid3x

    Mid3x

    Joined:
    Jul 12, 2020
    Posts:
    7
    What do you mean? Sry i ca't understand.
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    I'm not sure how I can be much clearer.

    Check the link, compare your OnTriggerEnter with theirs.

    Capitalize your start method to Start.
     
  10. Mid3x

    Mid3x

    Joined:
    Jul 12, 2020
    Posts:
    7
    I fixed the start problem but ican't understnd what's wrong with the OnTriggerEnter. Is it because i must make another void function with the playerprefs in it end then call it in the OnTriggerEnter? The only thing i see is that in the link he puts "Collider" in the () but with me it worked in the past without putting anything in the ().
     
  11. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    Even if you think it worked in the past, did you try it the way it's shown in their example?

    Did you add a debug message to the method to make sure it's not being triggered?

    Did you verify your colliders are setup correctly?
     
    Kurt-Dekker and Joe-Censored like this.
  12. Mid3x

    Mid3x

    Joined:
    Jul 12, 2020
    Posts:
    7
    So, i checked if the trigger works with a debug.log and also the collider, both seems to be working fine. I made a test with the int in playerprefs and instead of checking whether or not to load the next level, it activate or deactivate the corresponding level button based on the int value. Now the button is always disabled, so I think that the int does not change and that is the problem.