- Home /
locking levels based on new game
Hey everyone! I have been working on this game for a while, and I have a script set up for level selection. So far, it works great, but I am running into one snag. I play the game and make it through a few levels. However, if I try to hit New Game, all of the levels I have played stay unlocked. Basically, the locks won't reset and I don't know how to fix it. Anyone have any ideas of what I can do to get this fixed?
public class LevelSelectManager : MonoBehaviour {
public string[] levelTags;
public GameObject[] locks;
public bool[] levelUnlocked;
public string[] levelName;
private bool isPressed;
public Button[] button;
void Start()
{
GetComponent<Button>();
for(int i = 0; i < levelTags.Length; i++)
{
if(PlayerPrefs.GetInt(levelTags[i]) == null)
{
levelUnlocked[i] = false;
button[i].interactable = false;
}
else if (PlayerPrefs.GetInt(levelTags[i]) == 0)
{
levelUnlocked[i] = false;
button[i].interactable = false;
}
else
{
levelUnlocked[i] = true;
button[i].interactable = true;
}
if (levelUnlocked[i])
{
button[i].interactable = true;
locks[i].SetActive(false);
}
}
}
public void LoadScene(string levelName)
{
Application.LoadLevel(levelName);
}
}
And yes, if you are a newbie who is trying to figure this stuff out, you are more than welcome to use this script!
Answer by jasonlu00 · Sep 03, 2017 at 11:35 PM
When you unlock your level, you need to save the changes to PlayerPref as well:
private void UnlockLevel(int index){
levelUnlocked[index] = true;
button[index].interactable = true;
locks[index].SetActive(false);
PlayerPrefs.SetInt(levelTags[index], 1);
}
Hey thanks for your quick reply! I am going to try and find the PlayerPref, but I am actually unable to find that script. I looked in H$$anonymous$$CU\Software[company name][product name], but it wasn't in there anywhere. Any idea where else I could find it?
PlayerPrefabs is only saved to disk when you quit the game. You may have to use PlayerPrefabs.Save().