Quiz Game Tutorial Assistance

Updated on September 8, 2018 in [A] Tutorials
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on September 8, 2018

Hello,

I am trying to alter the quiz game from true/false to multiple choice.

This is what I edited the Questions script to look like: 

[System.Serializable]

public class Questions 
{
    public string question;
    public bool isTrue;
    public int questionValue;
    public float[] answerFloat;
    public string[] answerString;
}

And this is what the GameManager script looks like:

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

public class GameManager : MonoBehaviour 
{
    public Questions[] questions;
    private static List<Questions> unansweredQuestions;

    private Questions currentQuestion;

    [SerializeField]
    private Text questionText;

    [SerializeField]
    public int playerChoice;

    [SerializeField]
    private float delay = 1f; 

    [SerializeField]
    private Text popUpText;

    public Text[] buttonsText;

    void Start()
    {

        popUpText.enabled = false;
        if (unansweredQuestions == null || unansweredQuestions.Count == 0)
        {
            unansweredQuestions = questions.ToList<Questions>();
        }

        SetCurrentQuestion();
        SetCurrentAnswers();
    }

    private void Update()
    {
        if (playerChoice > 0)
        {
            AnswerSelected();
        }
    }

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

        questionText.text = currentQuestion.question;
    }

    void SetCurrentAnswers()
    {
        foreach (string item in currentQuestion.answerString)
        {
            Debug.Log(item);
        }
    }

    public int AnswerSelected()
    {
        if(currentQuestion.questionValue == playerChoice)
        {
            popUpText.text = “Correct”;
            popUpText.enabled = true;
        }

        else if (currentQuestion.questionValue != playerChoice)
        {
            popUpText.text = “Incorrect”;
            popUpText.enabled = true;
        }
        playerChoice = 0;
        return playerChoice;
    }

    public void PlayerChoice1()
    {
        playerChoice = 1;
        StartCoroutine(TransitionToNextQuestion());
    }

    public void PlayerChoice2()
    {
        playerChoice = 2;
        StartCoroutine(TransitionToNextQuestion());
    }

    public void PlayerChoice3()
    {
        playerChoice = 3;
        StartCoroutine(TransitionToNextQuestion());
    }

    public void PlayerChoice4()
    {
        playerChoice = 4;
        StartCoroutine(TransitionToNextQuestion());
    }

    public void PlayerChoice5()
    {
        playerChoice = 5;
        StartCoroutine(TransitionToNextQuestion());
    }

    IEnumerator TransitionToNextQuestion()
    {
        unansweredQuestions.Remove(currentQuestion);

        yield return new WaitForSeconds(delay);

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

The issue I am having is assigning each text in buttonsText[] a different string (in order) of the currentAnswer.answerString[]. Inside the SetCurrentAnswers() it properly displays each of the 5 options I have for one of my questions in the debug menu.

My idea was a foreach loop to assign each string in the answerString[] to a temporaryVariable, then assign the buttonsText[] = temporaryVariable. Then clear the variable and cycle through the answerString[] and buttonsText[] until complete. But it will not let me implicitly convert string[] to string or a string to UnityEngine.UI.Text[].

Does anyone have an idea on how to complete this or a better way to go about this that I am not thinking of?

  • Liked by
Reply
0 on September 8, 2018

So…I got it to work…this is how: 

  1. changed text button array to 5 individual text buttons
  2. Changed my SetCurrentAnswers() to:
    buttonsText1.text = currentQuestion.answerString[0];
    buttonsText2.text = currentQuestion.answerString[1];
    buttonsText3.text = currentQuestion.answerString[2];
    buttonsText4.text = currentQuestion.answerString[3];
    buttonsText5.text = currentQuestion.answerString[4];
  3. Finally, moved the SetCurrentAnswers() to the end of SetCurrentQuestion()

However, if you still see something in my code that could be improved please feel free to let me know!

  • Liked by
Reply
Cancel