- Home /
random range can stop when the index matches the input and without duplicate question display
hi i need help with random range. in this code i have an array to store questions. i want to make it appear random when the game start. questionIndex = Random.Range(0, questionPool.Length); this managed to randomly but there is still duplication and does not stop right according to the index. please help me
public SimpleObjectPool answerButtonObjectPool;
public Text questionText;
public Text scoreDisplay;
public Text scoreDisplay2;
public Text scoreDisplay3;
public Text timeRemainingDisplay;
public Transform answerButtonParent;
public GameObject questionDisplay;
public GameObject roundEndDisplay;
public GameObject GameOverDisplay;
public GameObject UIDisplay;
public Text highScoreDisplay;
private DataController dataController;
private RoundData currentRoundData;
private QuestionData[] questionPool;
private bool isRoundActive = false;
private float timeRemaining;
private int playerScore;
private int questionIndex;
private List<GameObject> answerButtonGameObjects = new List<GameObject>();
void Start()
{
audioPlayer = gameObject.AddComponent<AudioSource>();
dataController = FindObjectOfType<DataController>();
currentRoundData = dataController.GetCurrentRoundData();
questionPool = currentRoundData.questions;
timeRemaining = currentRoundData.timeLimitInSeconds;
UpdateTimeRemainingDisplay();
Time.timeScale = 1f;
playerScore = 0;
questionIndex = 0;
ShowQuestion();
isRoundActive = true;
}
void Update()
{
if (isRoundActive)
{
timeRemaining -= Time.deltaTime;
UpdateTimeRemainingDisplay();
if (timeRemaining <= 0f)
{
GameOverRound();
}
}
}
void ShowQuestion()
{
RemoveAnswerButtons();
questionIndex = Random.Range(0, questionPool.Length);
QuestionData questionData = questionPool[questionIndex];
questionText.text = questionData.questionText;
for (int i = 0; i < questionData.answers.Length; i++)
{
GameObject answerButtonGameObject = answerButtonObjectPool.GetObject();
answerButtonGameObjects.Add(answerButtonGameObject);
answerButtonGameObject.transform.SetParent(answerButtonParent);
answerButtonGameObject.transform.localScale = Vector3.one;
AnswerButton answerButton = answerButtonGameObject.GetComponent<AnswerButton>();
answerButton.SetUp(questionData.answers[i]);
}
}
void RemoveAnswerButtons()
{
while (answerButtonGameObjects.Count > 0)
{
answerButtonObjectPool.ReturnObject(answerButtonGameObjects[0]);
answerButtonGameObjects.RemoveAt(0);
}
}
Is it possible your QuestionData has a duplicate question in it? It's worth a look! I would start by looking in the RoundData - maybe a dupe got in there.
do i need to send my project? I have a problem when the match starts, he does display questions randomly. but when I proceed to the next question, sometimes it stops at 2 questions only, and 8 other questions have not been resolved. or questions continue to appear more than 10 questions. and there are questions that appear repeatedly and the same.
sorry if my english so bad
That's up to you. If you bundle up your project, I can look at it. But I need to know exactly what it's supposed to do. When the game starts, it shows 1 question and continues for 9 more questions? 10 questions total? One question at a time?
Answer by Vega4Life · Dec 05, 2018 at 04:05 PM
First the issue:
void ShowQuestion()
{
RemoveAnswerButtons();
questionIndex = Random.Range(0, questionPool.Length);
Here we get a random question from our pool. First thing I checked was to see if the questions were being removed from the array after they had been picked. They aren't, thus the dupe situation.
if (questionPool.Length > questionIndex + 1)
{
questionIndex++;
ShowQuestion();
}
Here I noticed we are incrementing questionIndex, but quickly realized it didn't do anything because it gets overwritten when we get a new question. Thus why the game never ends sometimes. In fact, the game will only end (and keep giving you a question) when the index is one less the length of the question pool. Because (question.Length > questionIndex + 1). This is only false with 10 questions, when the questionIndex is randomly chosen to be 9.
Now a quick solution I tested and works fine (you can change it or fix it however you feel):
private List<QuestionData> questionPool;
void Start()
{
currentRoundData = dataController.GetCurrentRoundData();
questionPool = new List<QuestionData>(currentRoundData.questions);
I made questionPool a list. This allows us to remove the questions that are picked and easily know how many questions are left.
void ShowQuestion()
{
RemoveAnswerButtons();
questionIndex = Random.Range(0, questionPool.Count);
QuestionData questionData = questionPool[questionIndex];
// Remove question from list so we don't get it again
questionPool.Remove(questionData);
Change length to .count, and add a new line to remove the question we picked from the list.
// If last question was correct && we have questions, continue
if (isCorrect && questionPool.Count > 0)
{
ShowQuestion();
}
else
{
EndRound();
}
Make sure the last question was correct before continuing, and just check if the list has things in it.
I tested this code and hit replay a few times and it works fine. Up to you if you want to refactor it, but it gets the point across. Nevertheless, game looks great, keep it up!
Answer by riskaanisah · Dec 05, 2018 at 02:54 AM
It would be better if you exported the entire project - just having code will make it difficult to understand where things go, etc. Just zip the entire unity project if you can.
okay wait the project I made with English and Indonesian so you are not confused. the problem I mean I mean you can open it in the Folder Scene/$$anonymous$$uis/ Scene $$anonymous$$uis there are Persistent, $$anonymous$$enu, and Game scenes
Just wanted to let you know, I am looking at this and I do see the issue. :) Let me get an answer together for you soon.
Your answer
![](https://koobas.hobune.stream/wayback/20220612183348im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Random.range multiple instantiations without repetition 1 Answer
reload random scene 2 Answers
Randomly setting a boolean to true? 3 Answers
Semi-Random Or Engine 1 Answer