Finishing up my 2D quiz game [Last Problem]
I have problem with my true false quiz game. I'm still new at using unity. Here is the link to my problem that I write on forum because its more clear and easy to understand there. https://forum.unity.com/threads/finishing-up-my-2d-quiz-game.1129834/
But here is the summary:
I try to make the game end and show congratulation screen IF the player answer 20 question correctly but the unity keep freezing when I answer the 19th question. Even if I make the index to 10 or 5 for example, it freeze in 9th or 4th question. So basically it always freeze on the last 2 question.
I try to make the game end and show game over screen IF the player answer 1 question wrong. And the game over screen will also tell the last score that player currently have. But the problem is I still don't know how to make it happen.
Here is the scripts on my game that I think relevant to my game:
Result Script : The core of the game. To manage in game button, score, transition for next question, etc
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using UnityEngine.SceneManagement;
public class Result : MonoBehaviour { public Questions questions; public GameObject correctSprite; public GameObject incorrectSprite; public GameObject completeLevelUI;
public GameOverScreen gameOverScreen; public Scores scores; public Button trueButton; public Button falseButton; public UnityEvent onNextQuestion; void Start() { correctSprite.SetActive(false); incorrectSprite.SetActive(false); } public void ShowResults(bool answer) { correctSprite.SetActive(questions.questionsList[questions.currentQuestion].isTrue == answer); incorrectSprite.SetActive(questions.questionsList[questions.currentQuestion].isTrue != answer); if (questions.questionsList[questions.currentQuestion].isTrue == answer) scores.AddScore(); else scores.DeductScore(); trueButton.interactable = false; falseButton.interactable = false; StartCoroutine(ShowResult()); } private IEnumerator ShowResult() { yield return new WaitForSeconds(1.0f); correctSprite.SetActive(false); incorrectSprite.SetActive(false); trueButton.interactable = true; falseButton.interactable = true; onNextQuestion.Invoke(); } public void CompleteLevel () { completeLevelUI.SetActive(true); } }
Questions Script: To store question
using System.Collections; using System.Collections.Generic; using UnityEngine;
[CreateAssetMenu] [System.Serializable]
public class Questions : ScriptableObject { [System.Serializable]
public class QuestionData { public string question = string.Empty; public bool isTrue = false; public bool questioned = false; } public int currentQuestion = 0; public List<QuestionData> questionsList; public void AddQuestion() { questionsList.Add(new QuestionData()); } }
QuestionsData Script : To manage question in game.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class QuestionsData : MonoBehaviour { public Questions questions; [SerializeField] private Text _questionText;
void Start() { AskQuestion(); } public void AskQuestion() { var randomIndex = 0; do { randomIndex = UnityEngine.Random.Range(0, 20); } while (questions.questionsList[randomIndex].questioned == true); questions.currentQuestion = randomIndex; questions.questionsList[questions.currentQuestion].questioned = true; _questionText.text = questions.questionsList[questions.currentQuestion].question; } public void ClearQuestion() { foreach (var question in questions.questionsList) { question.questioned = false; } } }
Scores Script : For in game scoring
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Scores : MonoBehaviour { public Text scoreText; private int _currentScore;
void Start() { _currentScore = 0; scoreText.text = _currentScore.ToString(); } public void AddScore() { _currentScore += 10; scoreText.text = _currentScore.ToString(); } public void DeductScore() { _currentScore = _currentScore > 0 ? _currentScore - 10 : 0; } }
Congratulation script : To create congratulation screen when the player answer 20 question correctly.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Congratulation : MonoBehaviour { public Result result;
void onTriggerEnter() { result.CompleteLevel(); } }
GameOverScreen Script : To make the game over when the player answer the question wrong and show the current score.
using System.Collections; using UnityEngine; using UnityEngine.UI;
public class GameOverScreen : MonoBehaviour { public Text pointsText;
public void Setup(int Scores) { gameObject.SetActive(true); pointsText.text = Scores.ToString(); } }
Please help. I'm still very new at this. This is actually the first game I ever made.