作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
几乎我在 Unity3D 中有一个我正在为 Android 制作的应用程序,您可以收集硬币,这是游戏的货币。每次我开始游戏时,我的硬币数量都会初始化为 0,即使我正在使用 setInt 和 getInt 来尝试保存硬币数量以供玩家下次玩游戏时使用。
完全不确定为什么它不保存 coinAmount,有什么想法吗? (可能有些愚蠢,因为我有点菜鸟)。
using UnityEngine;
using System.Collections;
public class pickupCoin : MonoBehaviour {
public int amountOfCoins;
public AudioClip coinPing;
public AudioSource playerAudio;
void Start () {
if(PlayerPrefs.GetInt("TotalCoinsPlayerPrefs") == null){
PlayerPrefs.SetInt("TotalCoinsPlayerPrefs", 0);
}
amountOfCoins = PlayerPrefs.GetInt("TotalCoinsPlayerPrefs");
}
void OnCollisionEnter(Collision other){
if(other.gameObject.name.Contains("coin")){
playerAudio.PlayOneShot(coinPing);
amountOfCoins+=1;
PlayerPrefs.SetInt("TotalCoinsPlayerPrefs", amountOfCoins);
Debug.Log("amount of coins is: " + amountOfCoins);
Debug.Log("Player Prefs Coin Amount Is" + PlayerPrefs.GetInt("TotalCoinsPlayerPrefs").ToString());
Destroy(other.gameObject);
}
}
最佳答案
您用来检查 PlayerPref 是否存在的测试 (PlayerPrefs.GetInt("TotalCoinsPlayerPrefs") == null
) 是“错误的”。该任务的正确方法是调用 HasKey功能。
基本上,我认为您当前的测试总是返回 TRUE,并且您的 PlayerPref 的内容总是在游戏开始时初始化为 0
。
关于c# - PlayerPrefs.SetInt 和 .GetInt 产生不良影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23197790/
我在使用 Heroku 时遇到问题,当我尝试部署我的应用程序时,它没有运行 run assets:precompile而且我还没有在本地预编译我的 Assets ,我在 Git 中没有 list 文件
(这是为您提供围绕我的问题的背景信息。您可以跳到“问题”并阅读该内容,然后如果您想直接进入主题,则可以返回并浏览背景知识.抱歉,这是一面文字墙!) 我需要将一堆非常非常糟糕的 JSON 存储在数据库中
我是一名优秀的程序员,十分优秀!