Update and Get PlayerPrefs during runtime?
Hello there,
So I am working on my ingame store where a user can buy a powerup. Once he purchases it, an integer value gets increased by one which is then stored to a PlayerPref. However, during gameplay, the script checks the if the amount of that integer becomes equal to or less than 0, if it does, it's supposed to disable it. But, this doesn't happen until I reset the scene.
Basically, the GetInt doesn't update in realtime. It only updates once I reset the scene. My idea is to disable the powerup once its amount reaches to 0 which is stored in a PlayerPref. I hope I made it clear. Is there any way I can do that?
Here's a piece of code that might be helpful
StoreUIManager
 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class StoreUIManager : MonoBehaviour {
     
 
     Animator backPanel;
     Animator confirmDialog;
     Animator notEnoughOrbsDialog;
 
     public Sprite invincibilityCell;
 
     public Image buyScreenItemIMG;
 
 
     //===PRICE LIST====//
     public int invincibilityOrbPrice = 10;
 
     //===============//
 
     //====ITEM BOOLS====//
     bool canPurchase;
     public bool isPurchasing;
 
 
 
     //================//
     public int invinPowerCount;
     public Text totalCoinsTXT;
     int orbCount;
 
 
     //initiliatizing other scripts
     public DataController dataControl;
 
     public void Start()
     {
         if (Application.loadedLevelName == "store") {
             dataControl = GameObject.Find ("DataController").GetComponent<DataController> ();
             checkNecessary ();
     
             backPanel = GameObject.Find ("dialog_bg").GetComponent<Animator> ();
             confirmDialog = GameObject.Find ("confirmdialog").GetComponent<Animator> ();
             notEnoughOrbsDialog = GameObject.Find ("notenoughorbs").GetComponent<Animator> ();
 
         
         }
 
 
     }
 
     public void Update()
     {    
         if (Application.loadedLevelName == "store") {
             totalCoinsTXT.text = PlayerPrefs.GetInt (dataControl.totalCoinsKEY).ToString ();
         }
     }
 
     public void checkNecessary()
     {
         orbCount = PlayerPrefs.GetInt (dataControl.totalCoinsKEY);
 
         if (orbCount >= invincibilityOrbPrice) {
 
             canPurchase = true;
 
         }
     }
 
     //purchase invincibility power
     public void buyInvincible()
     {
         if (canPurchase) {
             isPurchasing = true;
 
             buyScreenItemIMG.sprite = invincibilityCell;
 
             backPanel.SetBool("purchaseTriggered", true);
             confirmDialog.SetBool("confirmDialog", true);
 
 
         } else {
         
             backPanel.SetBool("purchaseTriggered", true);
             notEnoughOrbsDialog.SetBool("notEnoughOrbsTrigger", true);
             isPurchasing = false;
 
         }
     }
 
     //on BUY click
     public void buyBtnClick()
     {
         updateWallet (1);
         confirmDialog.SetBool ("confirmDialog", false);
     }
 
     //cancel button click
     public void cancelBtnClick()
     {
         backPanel.SetBool ("purchaseTriggered", false);
         confirmDialog.SetBool ("confirmDialog", false);
     }
 
     //okay button click
     public void okBtnClick()
     {
         notEnoughOrbsDialog.SetBool ("notEnoughOrbsTrigger", false);
     }
 
     //common btn functions
     public void btnCommon()
     {
     
     }
 
     //when backBtn is clicked
     public void backBtn()
     {
         Application.LoadLevel ("menu");
     }
 
     //update wallet
     public void updateWallet(int itemID)
     {
         totalCoinsTXT.text = (int.Parse (totalCoinsTXT.text) - invincibilityOrbPrice).ToString ();
 
         if (itemID == 1) {
             PlayerPrefs.SetInt(dataControl.invincibilityCountKEY, 1 + invinPowerCount);
             PlayerPrefs.SetInt (dataControl.totalCoinsKEY, dataControl.totalCoins - invincibilityOrbPrice);
             Application.LoadLevel ("store");
         }
 
         dataControl.updatePowers();
         
     }
 
 }
 
 
               DataController
 using UnityEngine;
 using System.Collections;
 using System.IO;
 
 
 public class DataController : MonoBehaviour {
 
     //importing character script
     public CharacterControl charScript;
     public StoreUIManager storeScript;
 
     public string totalCoinsKEY = "totalCoinsCount";
     public int totalCoins;
 
     public string bestScoreKEY = "bestScoreEver";
     public int bestScore;
     public bool updatePlayerBestScore;
 
     public string invincibilityCountKEY = "invinCount";
     public int invinPowerCount;
     
 
     // Use this for initialization
     void Start () {
     
         if (Application.loadedLevelName == "main") {
             //getting character script
             charScript = GameObject.Find ("character").GetComponent<CharacterControl> ();
             storeScript = GameObject.Find ("StoreUIManager").GetComponent<StoreUIManager>();
         }
 
         totalCoins = PlayerPrefs.GetInt (totalCoinsKEY);
 
         bestScore = PlayerPrefs.GetInt (bestScoreKEY);
 
         invinPowerCount = PlayerPrefs.GetInt (invincibilityCountKEY);
 
     }
 
 
     public void Update()
     {
 
 
     }
 
 
     public void playerCount()
     {
         //PlayerPrefs hmm
         PlayerPrefs.SetInt (totalCoinsKEY, charScript.orbs + totalCoins);
 
     }
 
     public void updatePlayerScore()
     {
 
 
         if (PlayerPrefs.HasKey (bestScoreKEY)) {
             
             if (PlayerPrefs.GetInt (bestScoreKEY) <= charScript.distance) {
                 
                 bestScore = PlayerPrefs.GetInt (bestScoreKEY);
                 
                 updatePlayerBestScore = true;
                 
             }
             
         }
 
         if(updatePlayerBestScore || !PlayerPrefs.HasKey(bestScoreKEY)) 
         {
             PlayerPrefs.SetInt (bestScoreKEY, charScript.distance);
             
         }
 
     }
 
     public void updatePowers()
     {
 
     }
 
 }
 
 
               MainUIManager using UnityEngine; using System.Collections; using UnityEngine.UI; using System.Collections.Generic; public class UIManager : MonoBehaviour {
 public AudioClip btnClick;
 public Animator startButton;
 public Animator aboutButton;
 //public Animator soundButton;
 public Animator logoImage;
 public Sprite soundOffSprite;
 public Sprite soundOnSprite;
 //after player die UI
 public Text distanceCovered;
 public Text coinsCollected;
 //public Text randomHints;
 public Text totalCoinsText;
 public Text randomHintsText;
 public Text bestScoreA;
 public string[] randomHints;
 public bool isMute;
 public bool isRestart;
 public Button powerInvin;
 //getting player script for variables
 public CharacterControl charScript;
 public DataController dataControl;
 public StoreUIManager storeScript;
 public void Start()
 {
     if (Application.loadedLevelName == "main") {
         GameObject player = GameObject.Find ("character");
         charScript = player.GetComponent<CharacterControl> ();
         randomHintsText.text = randomHints [Random.Range (0, randomHints.Length)];
         bestScoreA.text = dataControl.bestScore.ToString();
         storeScript = GameObject.Find("StoreUIManager").GetComponent<StoreUIManager>();
         if (dataControl.invinPowerCount <= 0) {
             powerInvin.enabled = false;
             powerInvin.GetComponentInChildren<CanvasRenderer>().SetAlpha(0.5f);
             
         } else {
             powerInvin.enabled = true;
         }
     
     }
     dataControl = GameObject.Find ("DataController").GetComponent<DataController> ();
     if (Application.loadedLevelName == "menu") {
         totalCoinsText.text = dataControl.totalCoins.ToString ();
     }
     //checking if invincibility power has even been bought or not.
 
 }
 public void Update()
 {
 
 }
 public void FixedUpdate()
 {
 }
 public void restartScreenUI()
 {
     distanceCovered.text = charScript.distLabel.text;
     coinsCollected.text = charScript.orbsLabel.text;
 }
 //start game event
 public void startGame()
 {
     aboutButton.SetBool ("isClicked", true);
     startButton.SetBool ("isClicked", true);
     //soundButton.SetBool ("isClicked", true);
     logoImage.SetBool ("isClicked", true);
     Application.LoadLevel ("main");
 
 }
 //about click event
 public void aboutGame()
 {
     aboutButton.SetBool ("isClicked", true);
     startButton.SetBool ("isClicked", true);
     //soundButton.SetBool ("isClicked", true);
     logoImage.SetBool ("isClicked", true);
 }
 //sound click event
 public void soundClick()
 {
 }
 //common BUTTON function
 public void btnCommon()
 {
     //playing sound on click
     AudioSource.PlayClipAtPoint (btnClick, transform.position, 2000.0f);
 }
 //restarting level
 public void restartLevel()
 {
     Application.LoadLevel ("main");
 }
 //exit to menu
 public void exitToMenu()
 {
     Application.LoadLevel ("menu");
 }
 //go to Store
 public void goToStore()
 {
     Application.LoadLevel ("store");
 }
 //invincibility power click
 public void powerInvinClick()
 {
     charScript.powerInvincibleEvent ();
     dataControl.invinPowerCount = dataControl.invinPowerCount - 1;
     PlayerPrefs.SetInt (dataControl.invincibilityCountKEY, dataControl.invinPowerCount);
 }
 
               }
Could you tell us which one of your three PlayerPrefs keys is the one which doesn't update? Which file/function/line does that call occur on? Which integer is being incremented?
Or actually, could you just print(PlayerPrefs.GetInt(...)); before and after you write to it so you can see for yourself if the value is updated or not. I am thinking the problem is not with PlayerPrefs but with your code. See this example, it works just fine...
 using UnityEngine;
 
 public class PlayerPrefsRealTime : $$anonymous$$onoBehaviour {
     public int output;
     void Update() {
         PlayerPrefs.SetInt("Time", (int)Time.timeSinceLevelLoad);
         output = PlayerPrefs.GetInt("Time");
     }
 }
 
                 Your answer
 
             Follow this Question
Related Questions
How to add colours to individual letters in an RPG text box scrolling 0 Answers
2D Roguelike part 5 - Code or script issue "Are you missing 'Complete' using directive" 1 Answer
Make a script of button which gets value of UI.Toggle and stores it in a file 2 Answers
2D Object effect like Life is Strange? 0 Answers
Unity jump from one canvas scene to another scene with canvas 2 Answers