Scene keeps reloading, can't keep score

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; 

}

I’m not sure why you need to reload the scene when you are going to the next question.
But try saving the score before loading the new scene:

SumScore.SaveHighScore();
SumScore.Reset();
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

instead of:

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
SumScore.SaveHighScore();
SumScore.Reset();

Event though you did not gave the code of SaveHighScore(), I’ll assume it can serialize the saved score properly.

You probably want to create your game manager instance in an initial “set up” scene, and within it’s Awake function, call DontDestroyOnLoad() in order to make sure it exists even between scenes so you don’t lose the data it holds.

Hope this helps!

create a score script like this
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class ScoreScript : MonoBehaviour {

public static int scoreValue = 0;
Text score;
// Use this for initialization
void Start () {
	score = GetComponent<Text> ();
}

// Update is called once per frame
void Update () {
	score.text = "Answered :" +scoreValue;
}

}
and in gamemanager in transition to next question before reloads like this
IEnumerator TransitionToNextQuestion()
{
unansweredQuestions.Remove(currentQuestion);

	yield return new WaitForSeconds(TimeBetweenQuestions);

	ScoreScript.scoreValue += 1;

     SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

}

hope this works for as the game is same on which i am working