我正在尝试所有可能的解决方案,但请看一下。显示分数,也显示高分,但高分始终与分数相同。当我重新开始游戏时,得分不是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;
}
这是我的播放器脚本,其中包含此类中的gameover方法
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 ();
}
问题解决了。只是改变这个&#34;积分=高分;&#34; TO&#34; highscore = points;&#34;。我不知道但是这很重要吗?我希望有人能解释一下。
答案 0 :(得分: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