Change the scene, keep gameobject values

Hello everyone, I am having trouble with my quiz type game, maybe someone knows what’s the solution. I have gameobject Quizmanager assigned to 4 buttons, if question is answered, the next question with answers is loaded. However, what I don’t know is how do I load the next scene, once the next question is loaded, but keep the same gameobject where all the questions are written. I have added images to show, what I mean. I have tried the function DontDestroyOnLoad(this), but I’m not sure whether it is possible to do so. Below is the script of Quizmanager. Any help is appreciated.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class QuizManager : MonoBehaviour
{
    public List<QuestionsAnswers> QnA;
    public GameObject[] options; 
    public int currentQuestion; 

    public GameObject Quizpanel;
    public GameObject GoPanel;

    public Text QuestionTxt;
    public Text ScoreTxt;

    int totalQuestions = 0;
    public int score;

    private void Awake()
    {
        DontDestroyOnLoad(this);
    }

    private void Start()
    {
        totalQuestions = QnA.Count;
        GoPanel.SetActive(false);
        generateQuestion();
    }

    public void retry()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    void GameOver()
    {
        Quizpanel.SetActive(false);
        GoPanel.SetActive(true);
        ScoreTxt.text = score + "/" + totalQuestions;
    }

    public void correct()
    {
        score += 1;
        QnA.RemoveAt(currentQuestion); 
        StartCoroutine(waitForNext());
 
    }

    public void wrong()
    {
        QnA.RemoveAt(currentQuestion);
        StartCoroutine(waitForNext());
    }

    IEnumerator waitForNext()
    {
        yield return new WaitForSeconds(3);
        generateQuestion();
    }

    void SetAnswers()
    {
        for (int i = 0; i < options.Length; i++) 
        {
            options_.GetComponent<Image>().color = options*.GetComponent<AnswerScript>().startColor;*_

options*.GetComponent().isCorrect = false;*
options_.transform.GetChild(0).GetComponent().text = QnA[currentQuestion].Answers*;*_

if (QnA[currentQuestion].CorrectAnswer == i + 1)
{
options*.GetComponent().isCorrect = true;*
}
}
}

void generateQuestion()
{
if (QnA.Count > 0)
{
currentQuestion = 0;

QuestionTxt.text = QnA[currentQuestion].Question; /
SetAnswers();
}
else
{
Debug.Log(“Out of Questions”);
GameOver();
}
}
}
Another script is QuestionsAnswers, which are used to assign values in the gameobject.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]

public class QuestionsAnswers
{
public string Question;
public string[] Answers;
public int CorrectAnswer;
}

You shouldn’t change the scene to load a new question. You just change your scene when the game is over and then you send the data you to save (like score) to a singleton with a DontDestroyOnLoad in its game object.

Watch this official tutorial by Unity that should get you up to speed.

As MSavioti touched upon, you should have DontDestroyOnLoad on the gameobject, not the script.
DontDestroyOnLoad(this); Refers to the script instance, but not the gameobject, so the script won’t be destroyed, but there won’t be any gameobject available in the scene for the script to sit on. Therefore, you should call DontDestroyOnLoad(gameObject); instead.

Hello mate. There actually is another way to go about it. Why don’t you try playerprefs? It’s especially very awesome because you can also then have high scores and stuff. Here is a brackeys link for you

This should work.

Thank you all for responses, DontDestroyOnLoad(gameObject);, and the same thing for Canvas, seems to to the trick I wanted. However, I have Timeline playable clips assigned to each button with OnClick function, and when the scene is changed, the functions on the buttons dissapear, is there a way to assign these functions to the buttons?173840-img3.png

Rather than use DontDestroyOnLoad, put all your data in a scriptable object, they do not live in scene so all data will stay when changing scenes.