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 /
  • Help Room /
avatar image
0
Question by PaincideStudio · Dec 27, 2017 at 01:16 PM · buttonplayerprefssaveload

About Playerprefs Save & Load

Hi.
I'm currently making 2d clicker game.
I'm having issue with Playerprefs.
What I'm trying to do is " Click [button A] and when [button A] is clicked, it becomes disabled and [button B] becomes enabled. When I quit my game, I want the button state saved "
Everything works fine except saving.
My game is played on the android, and it always saves when I quit my game.
But when I click my buttons and found them work good, it doesn't save the data.
I'm saving my data on the DataController(Which I made with Empty game object).


BGM Buying button script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class BGMBuy : MonoBehaviour {
 
     public AudioSource sound;
     public string bgmName;
     public Button BGM;
     public Button Disabled;
     public int currentCost;
     [HideInInspector]
     public int butdisable;
     public bool bought;
 
 
     void Start()
     {
         DataController.Instance.LoadBGMBuy (this);
         bought = false;
     }
 
     public void PurchaseMusic ()
     {
         if (DataController.Instance.gold >= currentCost) 
         {
             if (bought == false) 
             {
                 DataController.Instance.gold -= currentCost;
                 PlayerPrefs.SetInt ("butdisable", 1);
                 sound.Play ();
                 BGM.interactable = true;
                 Disabled.interactable = false;
 
                 DataController.Instance.SaveBGMBuy (this);
 
 
             }
 
             else 
             {
                 Disabled.interactable = true;
 
             }
         }
     
     }
 }




DataController script ( You need to look at LoadBGMBuy ~ SaveBGMBuy at the below. Everything works fine except that part)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DataController : MonoBehaviour {
 
 
     private static DataController instance;
 
 
     public static DataController Instance 
     {
         get 
         {
             if (instance == null) 
             {
                 instance = FindObjectOfType<DataController> ();
                 if (instance == null) 
                 {
                     GameObject container = new GameObject ("DataController");
                     instance = container.AddComponent<DataController> ();
                 }
             }
             return instance;
         }
     }
 
 
     private ItemButton[] itemButtons;
 
 
     public long gold 
     {
         get 
         {
             if (!PlayerPrefs.HasKey ("Gold")) 
             {
                 return 0;
             }
 
             string tmpGold = PlayerPrefs.GetString ("Gold");
             return long.Parse (tmpGold);
         }
             
         set
         { 
             PlayerPrefs.SetString ("Gold", value.ToString());
         }
     }
 
     public int goldPerClick
     {
         get
         {
             return PlayerPrefs.GetInt("GoldPerClick",1);
         }
 
         set
         { 
             PlayerPrefs.SetInt ("GoldPerClick", value);
 
         }
     }
     void Awake()
     {
         //PlayerPrefs.DeleteAll ();
 
 
         itemButtons = FindObjectsOfType <ItemButton> ();
     }
 
 
 
 
 
     public void LoadUpgradeButton(UpgradeButton upgradeButton)
     {
         string key = upgradeButton.upgradeName;
 
         upgradeButton.level = PlayerPrefs.GetInt (key + "_level", 1);
         upgradeButton.goldByUpgrade = PlayerPrefs.GetInt (key + "_goldByUpgrade", upgradeButton.startGoldByUpgrade);
         upgradeButton.currentCost = PlayerPrefs.GetInt (key + "_cost", upgradeButton.startCurrentCost);
     }
 
     public void SaveUpgradeButton(UpgradeButton upgradeButton)
     {
         string key = upgradeButton.upgradeName;
 
         PlayerPrefs.SetInt (key + "_level", upgradeButton.level);
         PlayerPrefs.SetInt (key + "_goldByUpgrade", upgradeButton.goldByUpgrade);
         PlayerPrefs.SetInt (key + "_cost", upgradeButton.currentCost);
     }
 
     public void LoadItemButton(ItemButton itemButton)
     {
         string key = itemButton.itemName;
 
         itemButton.level = PlayerPrefs.GetInt(key + "_level");
         itemButton.currentCost = PlayerPrefs.GetInt (key + "_cost", itemButton.startCurrentCost);
         itemButton.goldPerSec = PlayerPrefs.GetInt (key + "_goldPerSec");
 
         if (PlayerPrefs.GetInt (key + "_isPurchased") == 1) 
         {
             itemButton.isPurchased = true;
         } 
         else 
         {
             itemButton.isPurchased = false;
         }
     }
 
     public void SaveItemButton(ItemButton itemButton)
     {
         string key = itemButton.itemName;
 
         PlayerPrefs.SetInt(key + "_level",itemButton.level);
         PlayerPrefs.SetInt (key + "_cost", itemButton.currentCost);
         PlayerPrefs.SetInt (key + "_goldPerSec",itemButton.goldPerSec);
 
         if (itemButton.isPurchased == true) 
         {
             PlayerPrefs.SetInt (key + "_isPurchased", 1);
         } 
         else 
         {
             PlayerPrefs.SetInt (key + "_isPurchased", 0);
         }
         
     }
 
     public int GetGoldPerSec()
     {
         int goldPerSec = 0;
         for (int i = 0; i < itemButtons.Length; i++) 
         {
             goldPerSec += itemButtons [i].goldPerSec;
         }
 
         return goldPerSec;
     }
 
     public void LoadBGMBuy(BGMBuy bgmBuy)
     {
         string key = bgmBuy.bgmName;
 
         bgmBuy.butdisable = PlayerPrefs.GetInt (key + "bought");
 
         if(bgmBuy.bought == true)
         {
             PlayerPrefs.GetInt (key + "_bought",1);
         }
 
         else
         {
             PlayerPrefs.GetInt (key + "_bought",0) ;
         }
     }
 
     public void SaveBGMBuy(BGMBuy bgmBuy)
     {
         string key = bgmBuy.bgmName;
 
         PlayerPrefs.SetInt (key + "_bought", bgmBuy.butdisable);
 
         if(bgmBuy.bought == true)
         {
             PlayerPrefs.SetInt (key + "_bought",1);
         }
 
         else
         {
             PlayerPrefs.SetInt (key + "_bought",0) ;
         }
 
                 
     }
 }

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

134 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 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 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

How to save my gold when i quit the game 1 Answer

Saving item count with Playerprefs 1 Answer

Save button state using Playerprefs 0 Answers

How to save the state of Canvases/Toggles? 1 Answer

A Little Help With Player Prefs 0 Answers


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