So I have followed the tutorial of the Unity Quiz game and wondered that If i want the game to present only up to 10 questions but the pool of questions have 20 or so so this is the script from the tutorial that I have somewhat edited.
I have a script called Game Controller
void Start()
{
dataController = FindObjectOfType<DataController>(); // Store a reference to the DataController so we can request the data we need for this round
currentRoundData = dataController.GetCurrentRoundData(); // Ask the DataController for the data for the current round. At the moment, we only have one round - but we could extend this
questionPool = currentRoundData.questions; // Take a copy of the questions so we could shuffle the pool or drop questions from it without affecting the original RoundData object
timeRemaining = currentRoundData.timeLimitInSeconds; // Set the time limit for this round based on the RoundData object
UpdateTimeRemainingDisplay();
playerScore = 0;
questionIndex = 0;
ShowQuestion();
isRoundActive = true;
}
void ShowQuestion()
{
RemoveAnswerButtons();
QuestionData questionData = questionPool[questionIndex]; // Get the QuestionData for the current question
questionText.text = questionData.questionText; // Update questionText with the correct text
for (int i = 0; i < questionData.answers.Length; i ++) // For every AnswerData in the current QuestionData...
{
GameObject answerButtonGameObject = answerButtonObjectPool.GetObject(); // Spawn an AnswerButton from the object pool
answerButtonGameObjects.Add(answerButtonGameObject);
answerButtonGameObject.transform.SetParent(answerButtonParent);
answerButtonGameObject.transform.localScale = Vector3.one;
AnswerButton answerButton = answerButtonGameObject.GetComponent<AnswerButton>();
answerButton.SetUp(questionData.answers[i]); // Pass the AnswerData to the AnswerButton so the AnswerButton knows what text to display and whether it is the correct answer
}
}
The script that Hold the Questions and Answers
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class DataController : MonoBehaviour
{
public RoundData[] allRoundData;
void Start ()
{
DontDestroyOnLoad (gameObject);
SceneManager.LoadScene ("MenuScreen");
}
public RoundData GetCurrentRoundData()
{
return allRoundData [0];
}
}