gpt4 book ai didi

c# - PlayerPrefs.SetInt 和 .GetInt 产生不良影响

转载 作者:搜寻专家 更新时间:2023-11-01 07:59:29 24 4
gpt4 key购买 nike

几乎我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com