How Will my Score be Unaffected.
So i Created a Quiz game. it almost work but the line of code: SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
keeps resetting the score to zero. How will my score be unaffected by that line of code?
public Question[] questions; private static List unansweredquestions;
[SerializeField]
private Text factText;
[SerializeField]
private Text TrueAnswerText;
[SerializeField]
private Text FalseAnswerText;
[SerializeField]
private Animator animator;
[SerializeField]
private float TimeBetweenQuestions = 1f;
private Question currentquestion;
int Score;
public Text Scoretext;
void Start()
{
if (unansweredquestions == null || unansweredquestions.Count == 0 )
{
unansweredquestions = questions.ToList<Question>();
}
SetCurrentQuestion();
}
void Update()
{
Scoretext.text = "Score: " + Score.ToString();
}
void SetCurrentQuestion()
{
int randomQuestionIndex = Random.Range(0, unansweredquestions.Count);
currentquestion = unansweredquestions[randomQuestionIndex];
factText.text = currentquestion.fact;
if (currentquestion.isTrue)
{
TrueAnswerText.text = "CORRECT";
FalseAnswerText.text = "WRONG";
}
else
{
TrueAnswerText.text = "WRONG";
FalseAnswerText.text = "CORRECT";
}
}
IEnumerator TransitionTonextQuestion ()
{
unansweredquestions.Remove(currentquestion);
yield return new WaitForSeconds(TimeBetweenQuestions);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void UserSelectTrue()
{
if (currentquestion.isTrue)
{
animator.SetTrigger("True");
Score ++ ;
Debug.Log("CORRECT!");
}
else
{
animator.SetTrigger("True");
Debug.Log("WRONG!");
}
StartCoroutine(TransitionTonextQuestion());
}
public void UserSelectFalse()
{
if (!currentquestion.isTrue)
{
animator.SetTrigger("FALSE");
Score++;
Debug.Log("CORRECT!");
}
else
{
animator.SetTrigger("FALSE");
Debug.Log("WRONG!");
}
StartCoroutine(TransitionTonextQuestion());
}
}
Comment
Answer by Akeru90 · Feb 08, 2019 at 08:52 AM
Change variable: Int score
To
Public static int score
yes Persistence! here check out this video tutorial: ~ https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data?playlist=17117