Hi,
I am using an asset called ‘sumScore’ and I’m attempting to implement the score and high score system to my quiz game. The problem is, I can’t really figure out a way to check if the question has been answered correctly or not. The questions are all stored in a GameManager script, in a list. It appears that whenever an answer is selected, the animation plays and then it reloads the scene and loads in another question. Is it possible to somehow not reload the scene and keep the score, as it keeps resetting the score whenever another question pops up.
Here is the GameManager script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
public Question[] questions;
private static List<Question> unansweredQuestions;
private Question currentQuestion;
[SerializeField]
private Text questionText;
[SerializeField]
private float timeBetweenQuestions = 1f;
[SerializeField]
private Text trueAnswerText;
[SerializeField]
private Text falseAnswerText;
[SerializeField]
private Animator animator;
public float targetRatio = 9f / 16f;
void Start ()
{
if (unansweredQuestions == null || unansweredQuestions.Count == 0)
{
unansweredQuestions = questions.ToList<Question>();
}
SetQuestion();
Camera cam = GetComponent<Camera>();
cam.aspect = targetRatio;
}
void SetQuestion()
{
int randomQuestionIndex = Random.Range(0, unansweredQuestions.Count);
currentQuestion = unansweredQuestions[randomQuestionIndex];
questionText.text = currentQuestion.question;
if(currentQuestion.isTrue)
{
trueAnswerText.text = "Jūs esate teisūs.";
falseAnswerText.text = "Jūs esate neteisūs.";
}
else
{
trueAnswerText.text = "Jūs esate neteisūs.";
falseAnswerText.text = "Jūs esate teisūs.";
}
}
IEnumerator TransitionToNextQuestion()
{
unansweredQuestions.Remove(currentQuestion);
yield return new WaitForSeconds(timeBetweenQuestions);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
SumScore.SaveHighScore();
SumScore.Reset();
}
IEnumerator CorrectTransitionToNextQuestion()
{
unansweredQuestions.Remove(currentQuestion);
yield return new WaitForSeconds(timeBetweenQuestions);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
SumScore.Add(SumScore.Score);
}
public void UserSelectFact()
{
animator.SetTrigger("True");
StartCoroutine(TransitionToNextQuestion());
}
public void UserSelectFiction()
{
animator.SetTrigger("False");
StartCoroutine(TransitionToNextQuestion());
}
}
And the question script:
[System.Serializable]
public class Question {
public string question;
public bool isTrue;
}