繁体   English   中英

如何让分数重置

[英]How do I get the score to reset

一旦玩家死亡并开始游戏,分数不会重置并保持之前的分数。 我希望在玩家死亡或离开游戏后重置分数。 你怎么做到这一点?

public class ScoreScript : MonoBehaviour {

    public static int scoreValue = 0;
    Text score;

    // Use this for initialization
    void Start () {
        score = GetComponent<Text>();
    }

    // Update is called once per frame
    void Update () {
        score.text = "Score: " + scoreValue;
    }
}

score.text存储Score:的评估值,以及在scoreValue Update()时存储在scoreValue中的任何内容。 您的任何代码都scoreValue根据您显示的内容更新scoreValue

还要注意scoreValuestatic ,确保准确反映您的意图(例如scoreValueScoreScript类的属性还是它的每个实例?)。 请注意, score不是静态的,我希望它们都具有相同的行为(无论是静态还是非静态)。

即类似的东西

public class ScoreScript : MonoBehaviour {

public static int scoreValue = 0;
Text score;

// Use this for initialization
void Start () {
    score = GetComponent<Text>();
}

// Update is called once per frame
void Update () {
    score.text = "Score: " + scoreValue;
}

void ScoreReset() {
    scoreValue = 0;
}

void AddPoints(int points) {
    scoreValue += points;
}
}

如果您在玩家死后加载场景scoreValue将为 0 原因,因为我看到您在上面的代码中分配了 0。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM