Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ZilvinasKrasauskas · Sep 19, 2016 at 06:19 PM · android2dscorequiz

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


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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ifdef_ben · Sep 21, 2016 at 01:18 PM

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.

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 MidiPrefix · Sep 21, 2016 at 01:40 PM

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.

https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

Hope this helps!

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 AKASH135 · Jul 11, 2017 at 01:50 AM

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

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

6 People are following this question.

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

Related Questions

Score system 1 Answer

Carry over texts from one scene to another? 1 Answer

Displaying the score after a reset. 2 Answers

Scoring System between two different scene? 2 Answers

Why won't the Score reset? 2 Answers


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