![](/img/trans.png)
[英]How do I restart my score (reset static score) in reloading scene in unity?
[英]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
。
还要注意scoreValue
是static
,确保准确反映您的意图(例如scoreValue
是ScoreScript
类的属性还是它的每个实例?)。 请注意, 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.