- Home /
PlayerPrefs and Unlocking Scenes
Hi, I have just set up a player prefs variable that whenever a scene is loaded, sets the value of the int to the build index. Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
public class CheckLastScene : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Debug.Log(PlayerPrefs.GetInt("loadedLevel"));
PlayerPrefs.SetInt("loadedLevel", SceneManager.GetActiveScene().buildIndex);
}
}
But the problem is that the script that unlocks the buttons once the level is completed and you go to the level selection resets once I revisit or proceed to the next level. Here is the script(Sorry it's long):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class UnlockAll : MonoBehaviour
{
public Button[] buttons;
public InputField input;
public Text text;
public string answer;
public ChangeScenes script;
public InputField winText;
public Button b1;
public Button b2;
public Button b3;
public Button b4;
public Button b5;
public Button b6;
public Button b7;
public Button b8;
public Button b9;
public Button b10;
public Button b11;
public Button b12;
public Button b13;
public Button b14;
public Button b15;
public string win = "Demo Reached, More Levels Coming Soon!";
void Start()
{
Debug.Log(PlayerPrefs.GetInt("loadedLevel"));
}
void Update()
{
if (PlayerPrefs.GetInt("loadedLevel") >= 22)
{
b1.enabled = true;
b2.enabled = true;
b3.enabled = true;
b4.enabled = true;
b5.enabled = true;
b6.enabled = true;
b7.enabled = true;
b8.enabled = true;
b9.enabled = true;
b10.enabled = true;
b11.enabled = true;
b12.enabled = true;
b13.enabled = true;
b14.enabled = true;
b15.enabled = true;
}
answer = input.text;
if (answer == "UnlockAll")
{
b1.enabled = true;
b2.enabled = true;
b3.enabled = true;
b4.enabled = true;
b5.enabled = true;
b6.enabled = true;
b7.enabled = true;
b8.enabled = true;
b9.enabled = true;
b10.enabled = true;
b11.enabled = true;
b12.enabled = true;
b13.enabled = true;
b14.enabled = true;
b15.enabled = true;
Debug.Log("Levels Enabled");
}
if (PlayerPrefs.GetInt("loadedLevel") == 5)
{
Debug.Log("Level 2 Unlocked");
b1.interactable = true;
b2.interactable = true;
}
if (PlayerPrefs.GetInt("loadedLevel") == 6)
{
Debug.Log("Level 3 Unlocked");
b1.interactable = true;
b2.interactable = true;
b3.interactable = true;
}
if (PlayerPrefs.GetInt("loadedLevel") == 7)
{
Debug.Log("Level 4 Unlocked");
b1.interactable = true;
b2.interactable = true;
b3.interactable = true;
b4.interactable = true;
}
if (PlayerPrefs.GetInt("loadedLevel") == 8)
{
Debug.Log("Level 5 Unlocked");
b2.interactable = true;
b3.interactable = true;
b4.interactable = true;
b5.interactable = true;
}
if (PlayerPrefs.GetInt("loadedLevel") == 9)
{
Debug.Log("Level 6 Unlocked");
b2.interactable = true;
b3.interactable = true;
b4.interactable = true;
b5.interactable = true;
b6.interactable = true;
}
if (PlayerPrefs.GetInt("loadedLevel") == 10)
{
Debug.Log("Level 7 Unlocked");
b2.interactable = true;
b3.interactable = true;
b4.interactable = true;
b5.interactable = true;
b6.interactable = true;
b7.interactable = true;
}
if (PlayerPrefs.GetInt("loadedLevel") == 11)
{
Debug.Log("Level 8 Unlocked");
b2.interactable = true;
b3.interactable = true;
b4.interactable = true;
b5.interactable = true;
b6.interactable = true;
b7.interactable = true;
b8.interactable = true;
}
if (PlayerPrefs.GetInt("loadedLevel") == 12)
{
Debug.Log("Level 9 Unlocked");
b2.interactable = true;
b3.interactable = true;
b4.interactable = true;
b5.interactable = true;
b6.interactable = true;
b7.interactable = true;
b8.interactable = true;
b9.interactable = true;
}
if (PlayerPrefs.GetInt("loadedLevel") == 13)
{
Debug.Log("Level 10 Unlocked");
b2.interactable = true;
b3.interactable = true;
b4.interactable = true;
b5.interactable = true;
b6.interactable = true;
b7.interactable = true;
b8.interactable = true;
b9.interactable = true;
b10.interactable = true;
}
if (PlayerPrefs.GetInt("loadedLevel") == 14)
{
Debug.Log("Level 11 Unlocked");
b2.interactable = true;
b3.interactable = true;
b4.interactable = true;
b5.interactable = true;
b6.interactable = true;
b7.interactable = true;
b8.interactable = true;
b9.interactable = true;
b10.interactable = true;
b11.interactable = true;
}
}
}
Thanks in advance for anyone who helps as I am quite frustrated
Answer by Namey5 · Sep 28, 2020 at 10:08 PM
If you don't want progress to regress, then you should check that the new value isn't lower than the value that is already saved. That aside, there are a few things to point out here;
First off, I can't see any reason to have this stuff in Update - Update is called every frame, but all of this only needs to happen once at any one time. You also access the same variable from playerprefs many times; it would be worth caching that in a local variable and reusing that instead.
One other thing I would do is use an array of buttons rather than declare each button individually; you are already naming them with an index, so changing to an array wouldn't make much of a difference, however it would allow you to do things much more efficiently.
Here's how those changes would look;
public class CheckLastScene : MonoBehaviour
{
void Start()
{
int lastLoadedLevel = PlayerPrefs.GetInt ("loadedLevel");
int currentLevel = SceneManager.GetActiveScene().buildIndex;
Debug.Log(lastLoadedLevel);
//Make sure we are only making new progress
if (currentLevel > lastLoadedLevel)
PlayerPrefs.SetInt("loadedLevel", currentLevel);
}
}
And in the main class;
public class UnlockAll : MonoBehaviour
{
public Button[] buttons;
public InputField input;
public Text text;
public string answer;
public ChangeScenes script;
public InputField winText;
//'b1' = 'levelButtons[0]', 'b2' = 'levelButtons[1]', etc.
public Button[] levelButtons = new Button[15];
public string win = "Demo Reached, More Levels Coming Soon!";
void Start()
{
Debug.Log(PlayerPrefs.GetInt("loadedLevel"));
//Call this once when the level is loaded
UnlockLevels ();
}
void UnlockLevels()
{
//Store these for reuse
int level = PlayerPrefs.GetInt("loadedLevel");
answer = input.text;
if (level >= 22 || answer == "UnlockAll")
{
//Iterate through every button and enable them all
for (int i = 0; i < levelButtons.Length; i++)
levelButtons[i].interactable = true;
Debug.Log("Levels Enabled");
}
else if (level > 4 && level < 15) //If we are within the right range
{
//Iterate through every button up to the required one and enable them
for (int i = 0; i < level - 3; i++)
levelButton[i].interactable = true;
Debug.LogFormat ("Level {0} Unlocked", level - 2);
}
}
}