- Home /
Other
Purchase Level Unlock System
I am attempting to make a level unlock system for my game where the player uses in game currency to purchase levels. Right now I am trying to use PlayerPrefs to save my locked and unlocked levels, but am confused. Most of the tutorials I have come across use a level unlock system where the player completes 1 level to unlock the next. My game has one starting level unlocked, but then the player chooses the next level they would like to unlock if they have enough in game currency. I know I am close. If you could offer some advice or a tutorial you used I'd really appreciate it!
Here's my code:
using UnityEngine; using System.Collections; using UnityEngine .SceneManagement ; using UnityEngine .UI;
public class ChangeSceneManager : MonoBehaviour {
public static bool GameStarted=false;
public Button BoxingRinkBtn;
public Button LaboratoryBtn;
public Button lockedBoxingRinkBtn;
public Button lockedLaboratoryBtn;
//public Image unlockedBoxingRinkImg;
//public Image unlockedLaboratoryImg;
//private bool unlockedBoxingRink=false;
//private bool unlockedLaboratory=false;
private bool boxingRinkPurchased;
private bool laboratoryPurchased;
public GameObject boxingRinkPurchasePanel;
public GameObject AreYouSureYouWantBuyPanel;
//private int BRUnlocked = 1;
private int BRLocked = 0;
private int LabUnlocked = 1;
private int LabLocked = 0;
// Use this for initialization
void Start () {
PlayerPrefs.GetInt ("BoxingRinkUnlock");
if (BRLocked == 1) {
lockedBoxingRinkBtn.image.enabled = false;
lockedBoxingRinkBtn.enabled = false;
BoxingRinkBtn.enabled = true;
}
boxingRinkPurchasePanel.SetActive (false);
AreYouSureYouWantBuyPanel.SetActive (false);
}
// Update is called once per frame
void Update () {
if (boxingRinkPurchased == false) {
lockedBoxingRinkBtn.enabled = true;
PlayerPrefs.SetInt ("BoxingRinkUnlock", BRLocked);
}
if (boxingRinkPurchased == true) {
lockedBoxingRinkBtn.image.enabled = false;
lockedBoxingRinkBtn.enabled = false;
BoxingRinkBtn.enabled = true;
//BoxingRinkBtn.image = unlockedBoxingRinkImg;
//PlayerPrefs.SetInt ("BoxingRinkUnlock", BRLocked);
}
/*else {
PlayerPrefs.SetInt ("BoxingRinkUnlock", BRLocked);
}*/
}
public void MainMenu(){
Time.timeScale = 1.0f;
/*SceneFader1.instance.FadeIn("Title Menu");
StartCoroutine (Wait ());
SceneFader1.instance.FadeOut();
SceneManager.LoadScene ("Title Menu");*/
//SceneManager.LoadScene ("Title Menu");
//ScreenFader.instance.LoadLevel("Title Menu");
SceneFader.instance.FadeIn("Title Menu");
//StartCoroutine(ChangeLevel("Title Menu"));
}
public void HowToPlay(){
Time.timeScale = 1.0f;
SceneFader.instance.FadeIn ("How To Play Scene");
}
public void Play(){
Time.timeScale = 1.0f;
SceneFader.instance.FadeIn ("Gameplay");
}
public void StageSelect(){
Time.timeScale = 1.0f;
SceneFader.instance.FadeIn ("Stage Select");
}
public void BoxingRinkStage(){
SceneFader.instance.FadeIn ("How To Play Scene");
}
public void LockedBoxingRink(){
boxingRinkPurchasePanel.SetActive (true);
}
public void BuyBoxingRink(){
AreYouSureYouWantBuyPanel.SetActive (true);
}
public void YesPurchaseBoxingRink(){
boxingRinkPurchased = true;
BRLocked++;
boxingRinkPurchasePanel.SetActive (false);
AreYouSureYouWantBuyPanel.SetActive (false);
PlayerPrefs.SetInt ("BoxingRinkUnlock", BRLocked);
}
public void ExitAreYouSureYouWantToBuyBoxingRinkPanel(){
AreYouSureYouWantBuyPanel.SetActive (false);
}
public void ExitBuyBoxingRinkPanel(){
boxingRinkPurchasePanel.SetActive (false);
}
}
Answer by Asad-45 · Aug 06, 2016 at 02:55 AM
Well as a first you will want to initialize it by checking if the player is playing the game for first time like,
void Start() { if(PlayerPrefs.GetSInt("FirstTime") == 0) { PlayerPrefs.SetInt("FirstTime", 1); PlayerPrefs.SetInt("GameCurrency", 0) } } ^^ this removes any error that might be encountered when playing the game second time.
you`ll also want to set your PlayerPrefs like
private int GameCurrency
void Update() { PlayerPrefs.SetInt("GameCurrency", GameCurrency) }
so you can make changes to the declared variable in the update method without having to get it from the file each time, Hope this helped :)
Follow this Question
Related Questions
PlayerPrefs Problem crashing with SetBool 2 Answers
BoolPrefs Problem 2 Answers
boolean is true and false at a same time 2 Answers
Creating a Playerpref score system 1 Answer
determine the minimum time for the player to hold the button 1 Answer