- Home /
Getting random question at a maximum of 10 in a pool of question quiz game [help]
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. using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Collections.Generic;
public class AdditionGameController : MonoBehaviour { public AdditionSimpleObjectPool answerButtonObjectPool; public Text questionText; public Text scoreDisplay; public Text timeRemainingDisplay; public Transform answerButtonParent;
public GameObject questionDisplay;
public GameObject roundEndDisplay;
public Text highScoreDisplay;
private AdditionDataController dataController;
private AdditionRoundData currentRoundData;
private AdditionQuestionData[] questionPool;
private bool isRoundActive = false;
private float timeRemaining;
private int playerScore;
private int questionIndex;
private List<GameObject> answerButtonGameObjects = new List<GameObject>();
private List<int> unselectedIndices= new List<int> ();
void Start()
{
dataController = FindObjectOfType<AdditionDataController>(); // 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;
for (int i = 0; i < questionPool.Length; i ++) // For every AnswerData in the current QuestionData...
{
unselectedIndices.Add(i);
}
timeRemaining = currentRoundData.timeLimitInSeconds; // Set the time limit for this round based on the RoundData object
UpdateTimeRemainingDisplay();
playerScore = 0;
questionIndex = UnityEngine.Random.Range(0,unselectedIndices.Count);
ShowQuestion();
isRoundActive = true;
}
void Update()
{
if (isRoundActive)
{
timeRemaining -= Time.deltaTime; // If the round is active, subtract the time since Update() was last called from timeRemaining
UpdateTimeRemainingDisplay();
if (timeRemaining <= 0f) // If timeRemaining is 0 or less, the round ends
{
EndRound();
}
}
}
void ShowQuestion()
{
RemoveAnswerButtons();
AdditionQuestionData questionData = questionPool[unselectedIndices[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;
AdditionAnswerButton answerButton = answerButtonGameObject.GetComponent<AdditionAnswerButton>();
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
}
}
void RemoveAnswerButtons()
{
while (answerButtonGameObjects.Count > 0) // Return all spawned AnswerButtons to the object pool
{
answerButtonObjectPool.ReturnObject(answerButtonGameObjects[0]);
answerButtonGameObjects.RemoveAt(0);
}
}
public void AnswerButtonClicked(bool isCorrect)
{
if (isCorrect)
{
playerScore += currentRoundData.pointsAddedForCorrectAnswer; // If the AnswerButton that was clicked was the correct answer, add points
scoreDisplay.text = playerScore.ToString();
}
unselectedIndices.RemoveAt(questionIndex);
if(unselectedIndices.Count>0) // If there are more questions, show the next question
{
questionIndex= UnityEngine.Random.Range(0,unselectedIndices.Count);
ShowQuestion();
}
else // If there are no more questions, the round ends
{
EndRound();
}
}
private void UpdateTimeRemainingDisplay()
{
timeRemainingDisplay.text = Mathf.Round(timeRemaining).ToString();
}
public void EndRound()
{
isRoundActive = false;
questionDisplay.SetActive(false);
roundEndDisplay.SetActive(true);
}
public void ReturnToMenu()
{
SceneManager.LoadScene("MenuScreen");
}
}
did you find the answer for this? i have the same problem too
Answer by Ramsdal · Aug 30, 2018 at 01:12 PM
Im not quite sure what you are asking exactly, perhaps because i have not taken this tuturial. Do you just wish the random question selected to be among the first x questions in the pool?
questionIndex= UnityEngine.Random.Range(0,unselectedIndices.Count);
can then be changed to
questionIndex= UnityEngine.Random.Range(0,10);
This requires that you do indeed have 10 questions in the pool naturally! But if that is what you mean then this is the line you want to change.
Your answer
Follow this Question
Related Questions
How do I use LayerMask to check collision? 1 Answer
Trail of color behind the player 2D c# 1 Answer
[Quiz game] How do I make players contribute with questions? 1 Answer
Quiz Game Random Question Stuck 1 Answer
Immortal enemies when reanimated, or pooled game objects re-activated do not respond to collisions? 1 Answer