- Home /
The question is answered, right answer was accepted
Next Level to unlock when first level is completed
Hi,
I have a problem at the moment where I have a level unlock system that should unlock when a certain scene is closed. I got the code from a youtube video so I'm not sure where I need to edit it, there are 2 scripts that make up the system a level manager which governs the system overall, and a gamemanager which is applied to the scene which should unlock the next level.
The main issue I have is that the levels need to be called specific things, so in the code if you finish Level1 it unlocks level2. Also the first level unlocks by looking for level1, so normally loading and finishing level1 would unlock level 2. My difficulty is that I have multiple scenes that need to be completed for level2 to be unlocked. So I have level1, level1.2 and level1.3, the code unlocks level1 at start but will also unlock level2 as it checks that level1 has loaded to load level2, and I want level2 to start when I finish level1.3 and not level1.
Here are the relevant parts of the code for each script;
levelmanager;
[System.Serializable] public class Level { public string LevelText; public int UnLocked; public bool IsInteractable; } public GameObject levelButton; public Transform Spacer; public List<Level> LevelList; void Start () { FillList (); } void FillList() { foreach (var level in LevelList) { GameObject newbutton = Instantiate (levelButton) as GameObject; LevelButtonNew button = newbutton.GetComponent<LevelButtonNew> (); button.LevelText.text = level.LevelText; if (PlayerPrefs.GetInt ("Level" + button.LevelText.text) == 1) //this if statement unlocked the first level when a player opens the game for the first time { level.UnLocked = 1; level.IsInteractable = true; } button.unlocked = level.UnLocked; button.GetComponent<Button> ().interactable = level.IsInteractable; button.GetComponent<Button> ().onClick.AddListener (() => loadLevels ("Level" + button.LevelText.text )); //loads a level based on the name that the scene has been saved as if (PlayerPrefs.GetInt ("Level" + button.LevelText.text + "_score") > 0) { button.star1.SetActive (true); } if (PlayerPrefs.GetInt ("Level" + button.LevelText.text + "_score") > 5000) { button.star2.SetActive (true); } if (PlayerPrefs.GetInt ("Level" + button.LevelText.text + "_score") > 9999) { button.star3.SetActive (true); } newbutton.transform.SetParent (Spacer, false); } SaveAll (); } void SaveAll () { { GameObject[] allbuttons = GameObject.FindGameObjectsWithTag ("LevelButton"); foreach (GameObject buttons in allbuttons) { LevelButtonNew button = buttons.GetComponent<LevelButtonNew> (); PlayerPrefs.SetInt ("Level" + button.LevelText.text, button.unlocked); } } } void loadLevels(string value) { SceneManager.LoadScene (value); } }
gamemanager;
public Text scoreText; public int score = 10; private int LevelAmount = 7; //this needs to be updated if the level count changes private int CurrentLevel; void Start () { CheckCurrentLevel (); } void CheckCurrentLevel() { for (int i = 1; i < LevelAmount; i++) { if (SceneManager.GetActiveScene().name == "Level" + i) { CurrentLevel = i; SaveMyGame (); } } } void SaveMyGame() { int NextLevel = CurrentLevel + 1; if (NextLevel < LevelAmount) { PlayerPrefs.SetInt ("Level" + NextLevel.ToString(), 1);//unlock next level PlayerPrefs.SetInt ("Level" + CurrentLevel.ToString () + "_score", score); } else { PlayerPrefs.SetInt ("Level" + CurrentLevel.ToString () + "_score", score); } } }
There may be some stray syntax because I cut code out so don't worry about that. I'm really at a loss with how to do this, I understand it's a lot to go through so any help or advice will be appreciated.
Answer by ethranes · Jul 25, 2016 at 06:41 AM
The issue was with the gamemanager like Nitin22 mentions which is in each scene, it's not a clean way that I fixed it but it works, the gamemanager is renamed level1manager, level2manager and so on, and the following value;
PlayerPrefs.SetInt ("Level" + NextLevel.ToString(), 1);//unlock next level
Is only set to 1 on the last scene that the player needs to reach in level one, all prior levels have the value set to 0 to stop level2 from becoming unlocked.
Thanks for the help Nitin22.
Answer by Nitin22 · Jul 24, 2016 at 02:53 PM
@ ethranes , I think in "GameManager" when "Start" function calls you check the current active scene which is 1 ,thereafter SaveMyGame function is called and in that you check your "nextlevel" which is 2 with "levelAmount" which is 7 less than levelAmount and you unlock the next level which is 2.