简体   繁体   English

unity3d如何在测验中得分

[英]How put score in a quizgame in unity3d

how can i maintain the score in my quiz game every time it change it's question it also reset the score to zero here is my method 我如何在每次问答游戏中保持分数不变,这是将分数重置为零的问题,这是我的方法

 public void UserSelectTrue()
{  
    animator.SetTrigger("True");
    if (currentQuestion.isTrue)
    {
        countScore = countScore + 2;
        SetScoreText();
        Debug.Log("Correct");
    }
    else
    {
        Debug.Log("Wrong");
    }     
    StartCoroutine(TransitionToNextQuestion());
}
public void UserSelectFalse()
{
    animator.SetTrigger("False");
    if (!currentQuestion.isTrue)
    {
        countScore = countScore + 2;
        SetScoreText();
        Debug.Log("Correct");
    }
    else
    {
        Debug.Log("Wrong");
    }
    StartCoroutine(TransitionToNextQuestion());
}

you can use Singleton pattern with DontDestroyOnload to keep only one instance of the object between the scenes, be aware of the fact that DontDestroyOnload means the object won't be destroyed when a new scene loads 您可以将Singleton模式与DontDestroyOnload一起使用,以仅在场景之间保留对象的一个​​实例,请注意以下事实: DontDestroyOnload意味着在加载新场景时不会破坏对象

 public class ScoreManager: MonoBehaviour
 {
     private static ScoreManager_instance ;

     void Awake()
     {
         //we will make an instance if we don't have one yet
         if(!_instance)
             _instance = this ;
         //if we have an instance we don't need this one so we Destroy it
         else
             Destroy(this.gameObject) ;

          //we can keep this object between the scense with DontDestroyOnLoad
         DontDestroyOnLoad(this.gameObject) ;
     }
 }

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

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