Quantcast
Channel: Questions in topic: "playerprefs"
Viewing all articles
Browse latest Browse all 1333

Unlocking Levels in the Menu with PlayerPrefs

$
0
0
I'm trying to figure out how to use PlayerPrefs correctly to unlock levels as the player progresses. Right now, the player can continue to the next level while playing, but as soon as they go back to the main menu, they are stuck at level 1 again. I can't figure out how to unlock the levels on the menu screen too. I don't have it where the player can see all the levels, but instead, the next or previous level button becomes active when the player hits an arrow button. The relevant part of my code is below. Anytime is says gm it references Game Master. Level Manager void Start() { int levelReached = PlayerPrefs.GetInt("levelReached", 1); LevelList = new GameObject[transform.childCount]; //Fill array with level buttons for (int i = 0; i < transform.childCount; i++) LevelList[i] = transform.GetChild(i).gameObject; //Toggle off renderer foreach (GameObject go in LevelList) { go.SetActive(false); } if(LevelList[0]) { LevelList[0].SetActive(true); } } public void levelDown() { for (int i = 0; i < transform.childCount; i++) { if (i + 1 < levelReached) { // Toggle off the current button LevelList[index].SetActive(false); index--; if (index < 0) index = LevelList.Length - 1; // Toggle on the new button LevelList[index].SetActive(true); } } } public void levelUp() { for (int i = 0; i < transform.childCount; i++) { if (i + 1 < levelReached) { // Toggle off the current button LevelList[index].SetActive(false); index++; if (index == LevelList.Length) index = 0; // Toggle on the new button LevelList[index].SetActive(true); } } } Game Master public void WinLevel() { PlayerPrefs.SetInt("levelReached", levelToUnlock); } ReachGoal public void LoadNewLevel() { gm.WinLevel(); SceneManager.LoadScene(currentLevel + 1); } Thank you!

Viewing all articles
Browse latest Browse all 1333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>