- Home /
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[i].GetComponent<Image>().color = options[i].GetComponent<AnswerScript>().startColor;
options[i].GetComponent<AnswerScript>().isCorrect = false;
options[i].transform.GetChild(0).GetComponent<Text>().text = QnA[currentQuestion].Answers[i];
if (QnA[currentQuestion].CorrectAnswer == i + 1)
{
options[i].GetComponent<AnswerScript>().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;
}
Answer by MSavioti · Jan 08, 2021 at 02:25 AM
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.
Thank you for your reply, as you said I have to change the scene, when the game is over, but how do I change the scene, when the game is not over, and keep the same gameobject with the same values, and load it in a new scene?
singletons are the devil.. this should be done with SOs
Answer by Llama_w_2Ls · Jan 08, 2021 at 09:36 AM
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.
Answer by SamarthSaxena · Jan 08, 2021 at 10:25 AM
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 https://m.youtube.com/watch?v=vZU51tbgMXk This should work.
Answer by unity_yrQDx_7_nM74sg · Jan 08, 2021 at 02:32 PM
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?
You can assign and remove a listener method to a button anytime, anywhere, like so:
public UnityEngine.UI.Button button;
// Start is called before the first frame update
void Start()
{
button.onClick.AddListener(Random$$anonymous$$ethod);
}
void Random$$anonymous$$ethod()
{
// Some method in your timeline player script.
}
Thanks for your response, yet I still can't assign the PlayableDirector gameobject to the buttons, which came from another scene with DontDestroyOnLoad, is it possible to do so? The timeline PlayableDirector is in the current scene, yet the code doesn't assign it automatically to the buttons. It shows an error: $$anonymous$$issingReferenceException: The object of type 'PlayableDirector' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.
You should not destroy the object.
Answer by logicandchaos · Jan 10, 2021 at 01:04 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613022852im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
When die - reset score from the last scene 2 Answers
DontDestroyOnLoad duplicate prevention code is deleting the original player 2 Answers
Best way to deal with DontDestroyOnLoad when returning back to the same scene? 1 Answer
keep static/global object through scenes 1 Answer
Need some help with transform.position and velocity of object. 1 Answer