unity3d 黄金被设置为一个值,而不是将该值添加到黄金

inkz8wg9  于 2023-03-03  发布在  其他
关注(0)|答案(1)|浏览(108)

我做了一个货币系统,遇到了一个我无法解决的问题。基本上,黄金被设置为价值,而不是增加黄金的价值。有人能帮忙解决这个问题吗
主脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class levelBeaten : MonoBehaviour
{
    public GameObject finishCollider;
    public GameObject finishPopup;
    public currencyHandler currency;

    private int sceneIndex;

    // Start is called before the first frame update
    void Start()
    {
        sceneIndex = SceneManager.GetActiveScene().buildIndex;
        finishPopup.SetActive(false);
        Time.timeScale = 1f;

        // Check if gold has already been claimed in this scene
        if (PlayerPrefs.HasKey("GoldClaimedInScene" + sceneIndex))
        {
            Debug.Log("Gold has already been claimed in scene " + sceneIndex);
            return; // Gold has already been claimed
        }

        // Gold has not been claimed, add it and save the claim
        int goldToAdd = sceneIndex == 1 ? 300 : sceneIndex == 2 ? 500 : 0;
        currency.AddGold(goldToAdd);
        PlayerPrefs.SetInt("GoldClaimedInScene" + sceneIndex, 1);
    }

    // Update is called once per frame
    void Update()
    {

    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            levelfinishedpopup();
        }
    }

    public void levelfinishedpopup()
    {
        finishPopup.SetActive(true);

        Animator animator = GetComponentInChildren<Animator>();

        animator.SetTrigger("popupAnimation");

        // Wait for the animation to finish playing
        float animationLength = animator.GetCurrentAnimatorStateInfo(0).length;
        StartCoroutine(WaitAndStopTime(animationLength));
    }

    private IEnumerator WaitAndStopTime(float waitTime)
    {
        // Wait for the specified time
        yield return new WaitForSeconds(waitTime);

        // Set the time scale to 0
        Time.timeScale = 0f;
    }

}

货币处理员:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class currencyHandler : MonoBehaviour
{
    public int gold = 0;
    public GameObject currencyUI;

    private TextMeshProUGUI currencyText;

    private const string GOLD_KEY = "gold";

    void Start()
    {
        currencyText = currencyUI.GetComponent<TextMeshProUGUI>();
        LoadGold();
    }

    void Update()
    {
        currencyText.text = "Gold: " + gold.ToString();
        if (gold < 0)
        {
            gold = 0;
        }
    }

    public void AddGold(int amount)
    {
        gold += amount;
        SaveGold();
    }

    private void SaveGold()
    {
        PlayerPrefs.SetInt(GOLD_KEY, gold);
        PlayerPrefs.Save();
    }

    private void LoadGold()
    {
        gold = PlayerPrefs.GetInt(GOLD_KEY, 0);
    }
}

我试过当玩家撞到终点线时,如果场景指数是1,它会给你400金,如果场景指数是2,它会给你500金,而且你只能从每个场景中得到一次金,但是它没有把值加到金上,而是设置了值。

fzsnzjdm

fzsnzjdm1#

尝试更改此设置:

void Start()
{
  currencyText = currencyUI.GetComponent<TextMeshProUGUI>();
  LoadGold();
}

改为:

void Awake()
{
  currencyText = currencyUI.GetComponent<TextMeshProUGUI>();
  LoadGold();
}

您需要在执行其他操作之前加载gold,为此,您需要使用Awake方法,因为它在Start方法之前被调用。

相关问题