简体   繁体   中英

How to end a looped quiz in C# (Unity)

I have a code for a quiz with questions that are either true or false. There are five questions. When all the questions have been answered I want the game to end, but right now it just continues looping.

I would guess that the easiest soultion is to check when unansweredQuestions is full, how do I do this?

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.SceneManagement;

public class QuestionManager : MonoBehaviour {

    public Question[] questions;
    private static List<Question> unansweredQuestions;
    private Question currentQuestion;

    [SerializeField]
    private Text factText;

    [SerializeField]
    private Text trueAnswerText;

    [SerializeField]
    private Text falseAnswerText;

    [SerializeField]
    private Animator animator;

    [SerializeField]
    private float timeBetweenQuestions = 1f;

    void Start()
    {
        if (unansweredQuestions == null || unansweredQuestions.Count == 0)
        {
            unansweredQuestions = questions.ToList<Question>(); 
        }
        SetCurrentQuestion();
    }

    void SetCurrentQuestion()
    {
        int randomQuestionIndex = Random.Range(0, unansweredQuestions.Count);
        currentQuestion = unansweredQuestions[randomQuestionIndex];

        factText.text = currentQuestion.fact;

        if (currentQuestion.isTrue)
        {
            trueAnswerText.text = "KORREKT";
            falseAnswerText.text = "FEL";
        } else
        {
            trueAnswerText.text = "FEL";
            falseAnswerText.text = "KORREKT";
        }


    }

    IEnumerator TransistionToNextQuestion ()

    {
        unansweredQuestions.Remove(currentQuestion);

        yield return new WaitForSeconds(timeBetweenQuestions);

        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

    }

    public void UserSelectTrue()
    {
        animator.SetTrigger("True");
        if (currentQuestion.isTrue)
        {
            Debug.Log("CORRECT");
        } else
        {
            Debug.Log("WRONG");

        }

        StartCoroutine(TransistionToNextQuestion());
    }

    public void UserSelectFalse()
    {
        animator.SetTrigger("False");
        if (!currentQuestion.isTrue)
        {
            Debug.Log("CORRECT");
        }
        else
        {
            Debug.Log("WRONG");

        }

        StartCoroutine(TransistionToNextQuestion());

    }
}

Why do you not iterate in your Start Method through the unansweredQuestions List? Like:

void Start()
{
    if (unansweredQuestions == null || unansweredQuestions.Count == 0)
    {
        unansweredQuestions = questions.ToList<Question>(); 
    }
    SetCurrentQuestion();

   foreach(Question currentQuestion in unansweredQuestions)
   {
      //do something with the currentQuestion
   }
}

To avoid duplication I would heavily recommend to summarize your 2 functions "UserSelectTrue" & "UserSelectFalse" to something like the following:

public void UserSelect(bool answer)
    {
        animator.SetTrigger(answer);
        if ((answer && currentQuestion.isTrue) || (!answer && !currentQuestion.isTrue) )
        {
            Debug.Log("CORRECT");
        } else
        {
            Debug.Log("WRONG");
        }

        StartCoroutine(TransistionToNextQuestion());
    }

From what I understood you can create a simple statement like

if(unansweredQuestions.Count == 0) { return; }

I hope this can help you. Good luck with the game. I would like to see the end result. :)

I see you got this from Brackeys lol Good channel. In order to end the quiz, you need to check if you have run out of questions before setting the next question something like this:

void SetCurrentQuestion()
    {

    if (unansweredQuestions.Count == 0)
        {
            EndQuiz();
            return;
        }

        int randomQuestionIndex = Random.Range(0, unansweredQuestions.Count);
        currentQuestion = unansweredQuestions[randomQuestionIndex];

        factText.text = currentQuestion.fact;

        if (currentQuestion.isTrue)
        {
            trueAnswerText.text = "KORREKT";
            falseAnswerText.text = "FEL";
        } else
        {
            trueAnswerText.text = "FEL";
            falseAnswerText.text = "KORREKT";
        }
    }

You are not checking if it is full, rather you should check if it is empty since in the TransitionToNextQuestion() you are removing your currentQuestion from the unanseredQuestions list. Therefore when it is empty, the user has answered all of the questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM