- Home /
I can't save my money? :<
Hello friends, I writed a basic script but I can't save my money(like score). I'm not want best score or something. What is wrong?
Thats the basic money script;
static var moneys = 0;
function Update()
{
PlayerPrefs.GetInt("dollarf");
PlayerPrefs.SetInt("dollarf",moneys);
}
And thats the Int sender to text script;
var dollarText : UnityEngine.UI.Text;
function Update () {
dollarText.text = "Money " + PlayerPrefs.GetInt("dollarf");
}
When I adding money with this script;
function Update () {
if(Input.GetKey(KeyCode.L)){
dollarsystem.moneys +=10;
}
}
I only see when I back to menu, It's saved. But when I stop playing and restarting the game It's going.. What's wrong?
Answer by SeasiaInfotechind · Mar 17, 2015 at 06:50 AM
Hi, Your problem here is in this code
static var moneys = 0; // Sets the value zero
function Update()
{
PlayerPrefs.GetInt("dollarf"); // fetching the value....lets say its 50
PlayerPrefs.SetInt("dollarf",moneys); //Now passing the value of money which is 0 so the value in dollarf becomes zero.
}
Here you are supposed to do something like this.. so that your stored value is not disturbed.
PlayerPrefs.SetInt("dollarf",( PlayerPrefs.GetInt("dollarf")+moneys));
Hope you have understood the problem.
Your answer
Follow this Question
Related Questions
Does anyone know why continuously saving to playerprefs would cause a performance drop ? 0 Answers
Playerprefs resetting after quit 2 Answers
Is PlayerPrefs.Save() necessary ? (My game saves without it) 2 Answers
How to save gameobject values with respect to scene? 1 Answer
Mac: PlayerPrefs not deleted 1 Answer