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;
}