- Home /
Need help with level unlock system.
I have a level unlock system for 10 levels so far. And it works well at first, but if you go to a level you have passed after playing for a while, you lose your progress. Example: if you passed level 8 and unlocked level 9 you can play level 9. But if you go to menu, and select level 3, and pass level 3, you can now only play levels 1-4. And levels 5-9 which were previously unlocked are now locked again. (Locked = Disabled) This is my level selector code:
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LEVELSELECTOR : MonoBehaviour
{
public class LevelSaver : MonoBehaviour
{
//This function should be called through the button's OnClick
public void SaveLevel(int id){
PlayerPrefs.SetInt("levelReached", id);
}
}
public EndTrigger endTrigger;
public GameManager gameManager;
public Button[] levelButtons;
void Start ()
{
int levelReached = PlayerPrefs.GetInt ("levelReached", 1);
for (int i = 0; i < levelButtons.Length; i++)
{
if (i + 1 > levelReached)
levelButtons[i].interactable = false;
if (PlayerPrefs.GetInt("levelReached") < gameManager.LevelToUnlock)
{
PlayerPrefs.SetInt("levelReached", gameManager.LevelToUnlock);
}
}
}
public void Level01()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void Level02()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 2);
}
public void Level03()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 3);
}
public void Level04()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 4);
}
public void Level05()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 5);
}
public void Level06()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 6);
}
public void Level07()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 7);
}
public void Level08()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 8);
}
public void Level09()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 9);
}
public void Level10()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 10);
}
}
Answer by rh_galaxy · Sep 05, 2021 at 01:02 PM
Where do you set gameManager.LevelToUnlock?
//doing this in a for loop may give the wrong behavior? I think you should remove it.
//if (PlayerPrefs.GetInt("levelReached") < gameManager.LevelToUnlock)
//{
// PlayerPrefs.SetInt("levelReached", gameManager.LevelToUnlock);
//}
You need to do PlayerPrefs.SetInt("levelReached", ...) only at the time you reach a level above the present, and nowhere else.
It is not obvious that SaveLevel() will run when you click on a button... move it to that place. Every time you do a Set, you should also run this to make it stick after the program is exited.
PlayerPrefs.Save();
Answer by ianelirod2005 · Sep 06, 2021 at 03:15 AM
@rh_galaxy PlayerPrefs.SetInt("levelReached", ...) does go when you reach a level above the present. That works fine, the problem is when you go back to an earlier level, levels you had aleady reached become disabled. This is my game manager script. Reference to game manager is supoosedly going to this script.
using UnityEngine; using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
public int End = 1;
public string nextLevel = "Level02";
public int LevelToUnlock = 2;
public EndTrigger endTrigger;
bool gameHasEnded = false;
public float restartDelay = 1f;
public GameObject completeLevelUI;
public void CompleteLevel ()
{
completeLevelUI.SetActive(true);
PlayerPrefs.SetInt("levelReached", LevelToUnlock);
if (PlayerPrefs.GetInt("levelReached") < LevelToUnlock)
{
PlayerPrefs.SetInt("levelReached", LevelToUnlock);
}
if (LevelToUnlock > PlayerPrefs.GetInt("levelReached", 1))
{ PlayerPrefs.SetInt("levelReached", LevelToUnlock); }
}
public void EndGame ()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log("Game Over");
Invoke("Restart", restartDelay);
}
}
void Restart ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
If you follow my tip and remove all but one place where you have PlayerPrefs.SetInt("levelReached", ...) it will become easier, now you have it in five places and you have trouble.