PlayerPrefs not working

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!

using UnityEngine;
using UnityEngine.UI;


public class EndTrigger1 : MonoBehaviour
{
    int livello1Completed = 0;
    public GameManager gameManager;
  
  
    void OnTriggerEnter ()
    {
      
        PlayerPrefs.SetInt ("Livello1Completed", 1);
        gameManager.CompleteLevel1();
       
    }
      
  
}
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Livello2 : MonoBehaviour
{
    int livello1Completed;
    public float restartDelaydue = 1f;
    public void DelayLivellodue()
    {
        Invoke("Livellodue", restartDelaydue);
    }
    public void Livellodue()
    {
        livello1Completed = PlayerPrefs.GetInt ("Livello1Complete");
        if (livello1Completed == 1)
        SceneManager.LoadScene("Livello2");
  
    }

}

It works just fine, but check your spelling. Your SetInt string and your GetInt string don’t match.

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.

1 Like

Oh, that’s right. And i swear i checked everything more then once. Thank you so much guys!

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.

using UnityEngine;
using UnityEngine.UI;

public class EndTrigger1 : MonoBehaviour
{
    int livello1Completed = 0;
    const string Lvl1CompletedKey = "Livello1Complete";
    public GameManager gameManager;
   
    void start(){
        livello1Completed = PlayerPrefs.GetInt (Lvl1CompletedKey);
    }
    void OnTriggerEnter ()
    {
       
        PlayerPrefs.SetInt (Lvl1CompletedKey, 1);
        gameManager.CompleteLevel1();
       
    }
       
   
}
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Livello2 : MonoBehaviour
{
    int livello1Completed;
    const string Lvl1CompletedKey = "Livello1Complete";
    public float restartDelaydue = 1f;
    public void DelayLivellodue()
    {
        Invoke("Livellodue", restartDelaydue);
    }
    public void Livellodue()
    {
        livello1Completed = PlayerPrefs.GetInt (Lvl1CompletedKey);
        if (livello1Completed == 1)
        SceneManager.LoadScene("Livello2");
   
    }

}

Does anyone have any idea?

When using a Unity “magic method” it needs to be exactly what they have to run. Compare your version with theirs.

Also note that Unity’s magic methods must start with a capital letter, so your start is incorrect. If you want Unity to run it, it needs to be Start.

What do you mean? Sry i ca’t understand.

I’m not sure how I can be much clearer.

Check the link, compare your OnTriggerEnter with theirs.

Capitalize your start method to Start.

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 ().

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?

2 Likes

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.