- Home /
Weapon store using playerprefs does not retain stored information.
I'musing playerprefs to store and int value under the name of 'wep1', and have it initially set to 0. when purchasing the gun, the int value should become 1. The point of the shop is to allow the user to switch between guns as they choose in the store after a one time purchase, because if the int value in the playerprefs is supposed to be initially purchased if the 'wep1' value is set to one.
The problem is that it doesn't seem to be rewriting the valueto tell the playerprefs that the value is now 1, resulting in the gun having to be rebought over and over again, which is problematic.
var gun : String;
var purchased : boolean = false;
var cost : int = 0;
var money : int = 0;
var ID : int;
function Update(){
money = PlayerPrefs.GetInt("$$$");
}
function OnMouseDown(){
if(purchased == false){
if(PlayerPrefs.GetInt("$$$") >= cost){
money -= cost;
PlayerPrefs.SetInt("$$$", money);
purchased = true;
if(ID == 1){
PlayerPrefs.SetInt("wep1", 1);
}
}
if(purchased == true){
PlayerPrefs.SetString("PRI", gun);
}
}
}
function start(){
if(ID == 1){
if(PlayerPrefs.GetInt("wep1") == 1){
purchased = true;
}}
}
This is the reduced script (As the game has 27 total weapons), I'm wondering if anyone can tell me what is causing the error.
"$$$" may not be a valid name for a player preference value, you might want to call it "bank" or similar. Not sure if this is your problem. Are you sure the program gets to the line where it sets the "wep1" value? Try putting a Debug.Log message to make sure.
Figured out what the problem was, but upvoting your comment for being helpful too.
Answer by eeveelution8 · Aug 14, 2014 at 05:47 AM
The start function is labeled as,
function start(){
should be
function Start(){
Your answer
![](https://koobas.hobune.stream/wayback/20220613160556im_/https://answers.unity.com/themes/thub/images/avi.jpg)