Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 游戏重新开始时,为什么分数与高分相同?_C#_Android_Unity3d - Fatal编程技术网

C# 游戏重新开始时,为什么分数与高分相同?

C# 游戏重新开始时,为什么分数与高分相同?,c#,android,unity3d,C#,Android,Unity3d,我正在尝试所有可能的解决方案,但看看这个。显示了分数,也显示了高分,但高分始终与分数相同。当我重新开始游戏时,分数不是0,而是与高分相同。如果你得了6分作为高分,那么分数也是6分。我希望你能得到它 我拍了一张截图。它刚刚开始,我没有击中任何敌人。分数应为0,但与高分相同 这是附加到敌人的生命脚本: using UnityEngine; public class HealthScript : MonoBehaviour { public static HealthScript ins

我正在尝试所有可能的解决方案,但看看这个。显示了分数,也显示了高分,但高分始终与分数相同。当我重新开始游戏时,分数不是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;

}


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

看起来你这里有一个复制品

    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。嘿,突然,重新启动后不要更新分数@机器人克里斯,请你解释一下,它已经解决了。只需将此“分数=高分;”更改为“高分=分数;”。顺便说一句,谢谢!
    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