Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Fuffff quiz game, lets stop button selection after selecting right or wrong button :D

Discussion in 'Scripting' started by RNG-Development, Jun 22, 2023.

  1. RNG-Development

    RNG-Development

    Joined:
    Jun 15, 2018
    Posts:
    2
    Fuffff quiz game, lets stop button selection after selecting right or wrong button :D
    How to block it inside CheckAnswer guys? Now it is possible to select A B C D at the almost same time...

    using System.Collections.Generic;
    using TMPro;
    using UnityEngine;
    using UnityEngine.UI;

    public class QuizManager : MonoBehaviour
    {
    public TextMeshProUGUI questionText;
    public List<Button> answerButtons;

    private List<Question> questions;
    private Question currentQuestion;

    private void Start()
    {
    questions = QuestionData.GetQuestions();
    NextQuestion();
    }

    private void NextQuestion()
    {
    if (questions.Count == 0)
    {
    // No more questions left
    EndQuiz();
    return;
    }

    // Pick a random question + Button colours set to white
    foreach (Button button in answerButtons)
    {
    button.GetComponent<Image>().color = Color.white;
    }

    int randomIndex = Random.Range(0, questions.Count);
    currentQuestion = questions[randomIndex];
    questions.RemoveAt(randomIndex);

    // Update question text
    questionText.text = currentQuestion.question;

    // Update answer buttons
    for (int i = 0; i < answerButtons.Count; i++)
    {
    answerButtons.GetComponentInChildren<TextMeshProUGUI>().text = currentQuestion.answers;
    int index = i; // Capture the current value of i
    answerButtons.onClick.RemoveAllListeners();
    answerButtons.onClick.AddListener(() => CheckAnswer(index));

    }
    }

    private void CheckAnswer(int buttonIndex)
    {
    Button selectedButton = answerButtons[buttonIndex];

    if (currentQuestion.correctAnswerIndex == buttonIndex)
    {
    // Correct answer
    selectedButton.GetComponent<Image>().color = Color.green;
    selectedButton.GetComponentInChildren<TextMeshProUGUI>().text = "Correct Answer";
    Invoke("NextQuestion", 2f);
    }
    else
    {
    // Incorrect answer
    selectedButton.GetComponent<Image>().color = Color.red;
    selectedButton.GetComponentInChildren<TextMeshProUGUI>().text = "False Answer";
    Invoke("ResetQuiz", 1f);
    }
    }

    private void ResetQuiz()
    {
    // Reset the quiz after 5 seconds
    foreach (Button button in answerButtons)
    {
    button.GetComponent<Image>().color = Color.white;
    button.GetComponentInChildren<TextMeshProUGUI>().text = "";
    }

    questionText.text = "";
    NextQuestion();
    }

    private void EndQuiz()
    {
    // Quiz has ended
    // Add any necessary actions here
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,560
    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    You may edit your post above.