Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_yrQDx_7_nM74sg · Jan 07, 2021 at 10:47 PM · dontdestroyonloadscene-changevalues

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




img1.png (43.6 kB)
img2.png (43.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_yrQDx_7_nM74sg · Jan 08, 2021 at 06:09 AM 0
Share

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?

avatar image logicandchaos · Jan 10, 2021 at 01:05 PM 0
Share

singletons are the devil.. this should be done with SOs

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?alt text


img3.png (30.1 kB)
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Llama_w_2Ls · Jan 08, 2021 at 05:52 PM 0
Share

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.
     }
avatar image unity_yrQDx_7_nM74sg Llama_w_2Ls · Jan 08, 2021 at 08:39 PM 0
Share

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.

avatar image Llama_w_2Ls unity_yrQDx_7_nM74sg · Jan 08, 2021 at 10:03 PM 0
Share

You should not destroy the object.

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

115 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges