PlayerPrefs Not working

谁说胖子不能爱 提交于 2019-12-12 05:38:32

问题


Hey so im trying out unitys Playerprefs method and some how it wont save coins and when i close and exit the game it wont save it...

public Text CoinsText;
public int Coins;
public int clicks;


void Start()
{
    PlayerPrefs.GetInt("Coins", Coins);
}
void Update()
{   
    CoinsText.text = "Memes: " + Coins;
    if (Input.GetMouseButtonDown(0))
    {
        PlayerPrefs.SetInt("Coins", Coins);
        Coins += clicks;

    }
}

}


回答1:


You're never assigning to your Coins.

Try this in your Start():

Coins = PlayerPrefs.GetInt("Coins");

Note that the second value is only in case there wouldn't be a value saved.

public static int GetInt(string key, int defaultValue = 0);

Also, don't forget to Save all values before closing the program with:

PlayerPrefs.Save();



回答2:


First of all you should get int like this Coins = PlayerPrefs.GetInt("Coins");

Then after you SetInt you have to save it with PlayerPrefs.Save();



来源:https://stackoverflow.com/questions/40742286/playerprefs-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!