【问题标题】:Why is the score the same as the high score when the game restarts?为什么重新开始游戏时分数和高分一样?
【发布时间】:2018-04-29 13:16:44
【问题描述】:

我正在尝试所有可能的解决方案,但请看看这个。分数显示了,高分也显示了,但高分总是和分数一样。当我重新开始游戏时,分数不是0,而是和高分一样。如果你得到 6 分作为高分,那么分数也是 6 分。我希望你能明白。

我截图了。它刚刚开始,我没有击中任何敌人。分数应该是0,但是和高分一样。

这是附在敌人身上的健康脚本:

using UnityEngine;

public class HealthScript : MonoBehaviour
{

    public static HealthScript instance;
    public int hp = 1;
    private GUIText scoreReference;
    private GUIText highscoreReference;
    private static int _highscore = -1;
    public int highscore { 
        get { if (_highscore == -1) 
            _highscore = PlayerPrefs.GetInt("Highscore", 0);
            return _highscore; 
        }
        set {
            if (value > _highscore) {
                _highscore = value;
                highscoreReference.text = _highscore.ToString();
                PlayerPrefs.SetInt("Highscore", _highscore);
            }
        }
    }

    public bool isEnemy = true;


    private static int points;
    public void Damage(int damageCount) {
        hp -= damageCount;

        if (hp <= 0)
        {
            // Dead!
            Destroy(gameObject);
            points++;
            scoreReference.text = points.ToString();
        }
    }

    public void gameEnd() {

        points = highscore;
        points = 0;
    }

    //update from previous code
    void Start()
    {

    scoreReference = GameObject.Find("Score").guiText;
    highscoreReference = GameObject.Find("HighScore").guiText;
    scoreReference.text = points.ToString(); 
    highscoreReference.text = highscore.ToString ();
    instance = this;

}

这是我的播放器脚本,该类中的游戏结束方法

void OnDestroy()
{
    // Game Over.
    // Add the script to the parent because the current game
    // object is likely going to be destroyed immediately.
    transform.parent.gameObject.AddComponent ();
    HealthScript.instance.gameEnd ();
}

问题解决了。只需更改此“积分 = 高分;” TO“高分=积分;”。我不知道,但这样重要吗?我希望有人能解释一下。

【问题讨论】:

    标签: c# android unity3d


    【解决方案1】:

    您的此处似乎有重复项。

        scoreReference = GameObject.Find("Score").guiText;
        highscoreReference = GameObject.Find("HighScore").guiText;
        scoreReference.text = points.ToString(); //KEEP THIS
        highscoreReference.text = highscore.ToString();
        scoreReference = GameObject.Find("Score").guiText;
        scoreReference.text = highscore.ToString(); //REMOVE THIS - THIS IS A DUPLICATE
    

    【讨论】:

    • 谢谢,我很高兴能够解决这个问题。我还在 start 方法中删除了 points= 0 。
    • 突然,重启后不更新分数了。 @Droid 克里斯
    • 请详细说明一下
    • 已经解决了。只需更改此“积分 = 高分;” TO“高分=积分;”。顺便说一句,谢谢!
    猜你喜欢
    • 2017-01-02
    • 2015-07-22
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    相关资源
    最近更新 更多