【发布时间】:2014-04-21 12:49:23
【问题描述】:
几乎我在 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);
}
}
}
【问题讨论】: