用于高分计数的 CSharp 代码(Unity)有什么问题(higshscore 系统的行为就像“正常”分数计数系统)?

问题描述 投票:0回答:1

我正在使用 Unity 制作游戏,我尝试在游戏中添加高分,但高分总是重置为 0。 高分就像其他得分计数机制一样。

这是代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour
{
    public Text ScoreText;
    private float score;


    public Text highScore;

    void Start()
    {
        highScore.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
       
    }
    // Update is called once per frame
    void Update()
    {
        if (GameObject.FindGameObjectWithTag("Player") != null)
        {

            score += 1 * Time.deltaTime;
            ScoreText.text = ((int)score).ToString();

            if (score > PlayerPrefs.GetInt("Highscore", 0))
            {
                PlayerPrefs.SetInt("HighScore", (int)score);
                highScore.text = score.ToString("0");
            }

        }
    }
} 

高分计数部分无法正常工作。如果我开始新游戏,它总是重置为 0。

c# unity-game-engine unity3d-editor
1个回答
0
投票

您只是从未在

score
 中设置 
Start()

© www.soinside.com 2019 - 2024. All rights reserved.