Question by
ToukaKirishima1987 · Feb 05, 2018 at 04:58 PM ·
2dplayerprefslevels
Problems with my playerprefs. and Level Lock System
I have tried looking up specific codes on the internet but most of the answers i find are too old to use or out of my understanding. Here is some of my code that I think will work if I can figure out what's wrong with it. I am currently making a mobile 2D game so this is probably why it's confusing.
The issues I am having is that levels are all still clickable or you do not have to beat the level to unlock the next one, I placed Lock sprites to make them not interact with them. But this didn't really help lock them.
code
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelCompleted : MonoBehaviour
{
public string nextLevel = "Level 2";
public int levelToUnlock = 2;
public void LoadNextLevel()
{
int currentUnlock = PlayerPrefs.GetInt("You win!");
if (currentUnlock < levelToUnlock)
{
PlayerPrefs.SetInt("You win!", levelToUnlock);
}
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612152003im_/https://answers.unity.com/themes/thub/images/avi.jpg)