C# 如何在unity3d的问答游戏中得分
我如何在我的问答游戏中保持分数每次它改变它的问题它也重置分数为零这里是我的方法C# 如何在unity3d的问答游戏中得分,c#,unity3d,C#,Unity3d,我如何在我的问答游戏中保持分数每次它改变它的问题它也重置分数为零这里是我的方法 public void UserSelectTrue() { animator.SetTrigger("True"); if (currentQuestion.isTrue) { countScore = countScore + 2; SetScoreText(); Debug.Log("Correct"); } else
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());
}
您可以使用带有
DontDestroyOnload
的Singleton模式在场景之间仅保留一个对象实例,请注意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) ;
}
}
我不熟悉unity3d,但如果将
countScore
定义为静态,会发生什么?静态变量是全局变量,除非显式设置0,否则不会重置。静态变量应该可以正常工作。若你们不改变场景,那个么非静态应该是可行的。你们必须提供更多关于“TransitionOnNextQuestion”中发生了什么的信息?是否加载了另一个场景?是的,我正在重置场景,以便显示其他问题