how to use saved playerprefs value from one script to other script
i have a script for adding gold and i have to purchase accessories using that gold,for that i have to use saved playerprefs value in gold script to buyaccessories script. if(gold.store >= 10) here gold is script variable name and store has the last added gold value.i have to set the limit for gold too like if the gold amount is zero i cant buy any of accessories and gold amount should not go below zero.if this condition is true i can buy particular accessories. even if gold amount is below 10 or zero the button interactable should be off through script
Answer by Fockducker11 · Oct 21, 2017 at 08:22 AM
Try this. First create a script. No need to attach to any object. It just a script that will store your gold. Name the script "PlayerPrefsManager" or whatever name you want, but I like to name it PlayerPrefsManager. Write the code in the script as follow
const string GOLD_STORE_KEY = "gold_store"; //name whatever you want
public static void SetGoldStoreKey (int gold)
{
PlayerPrefs.SetInt (GOLD_STORE_KEY, gold);
// store your gold in the PlayerPrefs
}
public static int GetGoldStoreKey ()
{
return PlayerPrefs.GetInt (GOLD_STORE_KEY);
// get your stored gold to buy accessories
}
Show me your gold script and your accessories script so I can help you set it up.
Your answer
![](https://koobas.hobune.stream/wayback/20220612141437im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to get intent Data for Unity to use? 6 Answers
Platform Effector 2D not resseting 0 Answers
when ever i try to uplaod game on play store this error comes up !! pls help 0 Answers
How do I get an arrow to point towards an enemy in unity once instantiated? 1 Answer
Can VR projects created by Unity works on the web ? 0 Answers