C# _highScore.text在游戏结束时不更新

C# _highScore.text在游戏结束时不更新,c#,unity3d,C#,Unity3d,UIManager.cs:游戏结束后,我的高分不会更新。 在开始方法中,我获得先前的高分,在检查高分方法中,如果当前分数>先前分数,则高分将等于当前分数并保存它CheckForHighScore using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UIManager : MonoBehaviour { [Seri

UIManager.cs:游戏结束后,我的高分不会更新。 在
开始
方法中,我获得先前的高分,在
检查高分
方法中,如果当前分数>先前分数,则高分将等于当前分数并保存它<游戏结束后,将在Player.cs脚本中调用code>CheckForHighScore

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

public class UIManager : MonoBehaviour
{
    [SerializeField]
    private Text _scoreText;
    [SerializeField]
    private Text _highScoreText;

    public int currentScore;
    public int highScore;

    void Start()
    {
        _scoreText.text = "Score: " + 0;
        highScore = PlayerPrefs.GetInt("HighScore", 0);
        _highScoreText.text = "High Score: " + highScore;
    }

    public void UpdateScore()
    {
        currentScore+=10;
        _scoreText.text = "Score: " + currentScore.ToString();
    }

    public void CheckForHighScore()
    {
        if(currentScore > highScore)
        {
            highScore = currentScore; // if I debug.log here then it shows the high score correctly
            PlayerPrefs.SetInt("HighScore", highScore);
            _highScoreText.text = "High Score: " + highScore; // but here in the text it does not update
        }
    }
    
    public void GameOver(){
      _gameOverText.gameObject.SetActive(true);
      _restartText.gameObject.SetActive(true);
      StartCoroutine(FlickerRoutine());
      _gameManager.GameOver();
    }
// I have removed the methods that are not related to the score
}
Player.cs:

if (_lives < 1)
{
    _UIManager.CheckForHighScore(); // chech high score method from UIManager is called here
    _spawnManager.onPlayerDeath();
    _UIManager.GameOver();
    Destroy(this.gameObject);
}
if(_寿命<1)
{
_UIManager.CheckForHighScore();//这里调用UIManager中的chech HighScore方法
_spawnManager.onPlayerDeath();
_UIManager.GameOver();
摧毁(这个游戏对象);
}

阅读您的问题描述及其下面的注释,我发现您没有将高分保存在任何地方,以便在场景切换期间保持高分。 保持高分的正确方法可能是将一个组件分配给一个对象,仅用于处理这个值,您或您也可以保存这个值,但可能不是正确使用这个实用程序。

尝试使用.ToString();若要将highScore Integer值转换为string as+,运算符将添加两个变量,如果它们是数字,并且如果它们是字符串,它们将尝试加入。 或
只需将highScore值另存为字符串作为PlayerPerfs.SetString(“highScore”、“highScore”);如果使用此方法,则只需将类型更改为变量highScore的字符串。

\u UIManager.GameOver()中会发生什么?public void GameOver(){{u gameoverext.gameObject.SetActive(true);\u restartText.gameObject.SetActive(true);startcroutine(FlickerRoutine());\u gameManager.GameOver()}我删除了它,因为它在这里变得很长。好吧,那么
\u gameManager.GameOver()怎么办做什么?问题是:是否有可能正确的文本存在了一段时间,但随后发生了某种情况,以某种方式重置了文本?
public void GameOver(){u isGameOver=true;}
它将_isGameOver设置为true