我正在使用 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。
您只是从未在
score
中设置
Start()