简体   繁体   English

在 Unity 中重新启动后 PlayerPrefs 未保存

[英]PlayerPrefs not saving after restart in Unity

I have created a 2d top down shooter and have made the enemies drop coins when they die.我创建了一个 2d 自上而下的射击游戏,并让敌人在死亡时掉落硬币。 I want these coins to save to PlayerPrefs so that the players can use then in the shop.我希望将这些硬币保存到 PlayerPrefs 以便玩家可以在商店中使用。 My problem is that the amount of coins isn't saving when the game or editor is restarted.我的问题是重新启动游戏或编辑器时没有节省硬币数量。 Using the console I was able to narrow the problem down to the PlayerPrefs not saving.使用控制台,我能够将问题缩小到 PlayerPrefs 未保存。

public float playerCoins;
private float debugCoins;

void Update()
{
    PlayerPrefs.SetFloat("Bank", playerCoins);
    debugCoins = PlayerPrefs.GetFloat("Bank");
    PlayerPrefs.Save();
    Debug.Log(debugCoins);

I used the debugCoins variable and the Debug.Log to check if the variable is initially being saved before restarting the game, which it is.我使用 debugCoins 变量和 Debug.Log 来检查变量是否在重新启动游戏之前被保存,它是。

It is better to not use save methods on update, and you need to get your Bank on start, because you setting is playerCoins, before it loaded最好不要在更新时使用保存方法,并且您需要启动银行,因为您设置的是 playerCoins,然后才加载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM