如何使用统一重播得分为 0?

问题描述

我有猜谜游戏,这是最后一个分数出来的游戏。

分数是保存的,可以在游戏结束时显示,但是当我重玩游戏时,分数不会归零

这是我的问答代码

公共类问题答案:MonoBehavIoUr {
公共游戏对象Feedback_benar,Feedback_salah;

public void answer(bool QuestionAnswer){

    if (QuestionAnswer) {
        Feedback_benar.SetActive(false);
        Feedback_benar.SetActive(true);
        int skor = PlayerPrefs.GetInt ("skor") + 10;
        PlayerPrefs.SetInt ("skor",skor);  

    } else{
        Feedback_salah.SetActive(false);
        Feedback_salah.SetActive(true);
        
    }
    gameObject.SetActive (false);
    transform.parent.GetChild(gameObject.transform.GetSiblingIndex()+1).gameObject.SetActive (true);
    gameObject.SetActive (true);
}

这在我的乐谱脚本代码

公开课 Skor:MonoBehavIoUr

{

无效更新()

{
    GetComponent<Text> ().text = PlayerPrefs.GetInt ("skor").ToString();}}

解决方法

如果您希望每次玩测验时都重置分数,请不要保存它,但是如果您想实现高分系统,您可以这样做。

private float score;

private void Update()
{
    if (QuestionAnswered)
    {
        //Adds one to score if its right
        score++;
    }
}

void EndGame()
{
    // score only gets saved if it is higher than the previously saved highscore
    if (score > PlayerPrefs.GetFloat("HighScore",0f))
    {
        PlayerPrefs.SetFloat("HighScore",score);
    }
}

然后您只需在您希望游戏结束时调用 endgame 方法,它就会将高分与分数进行比较,如果分数大于保存的高分,它将被更新。