Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Aug 15, 2016 at 10:51 PM by El-Deiablo for the following reason:

Other

avatar image
0
Question by El-Deiablo · Aug 02, 2016 at 09:41 PM · buttonplayerprefsbooleanunlocklevel select

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);

 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

58 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges