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 Stormize · Oct 10, 2015 at 05:54 PM · c#2dguiplayerprefscodepage

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

 }


}

Comment
Add comment · Show 1
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
avatar image Statement · Oct 11, 2015 at 04:26 PM 0
Share

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

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

46 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

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


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