I am having problems with my script as I lack the skills to code in c#. I can seem to make my random question only appear once. Can you guys please help me? This is my code:
Why do you set the index range after you already grab the random question? move the questionIndex = Random.Range(0, questionPool.Length); above your QuestionData questionData = questionPool[questionIndex];
I canât exactly grasp what you mean, I am basically frankensteining my way out this. I am new to this coding thing. But I really appreciate you guys helping me, thanks by the way. what do you mean by âcontrolling the questionPool.Length is greater than 0?â
If the questionPool.Lenght (not Poo, sorry) is equal to 0, the Random.Range is always equal to 0, and the question displayed is always the same, or you get the âArray is out of rangeâ error
thanks man, and then what? What happens is when I randomly get to my last question, I tend to end the game, skipping all the other questions. I have a little knowledge on scripting as you can see. Appreciate the help man.
How is the game supposed to run? You say you get to your last question and then you canât get the rest to display⌠If itâs the last question then thatâs game over right?
OK, it looks to me like the problem is the âelseâ clause on lines 172-176. That (with the if-statement on line 164) basically says, if we just showed the last question, end the round. You were probably thinking âif we have no more questionsâ but since youâre picking questions in random order, it really means âif we happen to have just shown the last question on our list (regardless of whether there are others we havenât shown)â.
But actually, this gets to the deeper problem implied by your thread title, but not actually addressed here⌠you want to show these questions in a shuffled order, not just randomly pick a (possibly already-seen) question each time.
So itâs time to back up and rethink a little. The best way to do this is:
In Start, around line 50, shuffle your questions. Sadly C# does not have a built-in way to do this, so create a new script, delete the default stuff Unity gives you, and paste in the code from this answer. Then you should be able to just say new Random().Shuffle(questionPool) to get the job done.
In your ShowQuestion method, take out the Random.Range business. Instead simply increment questionIndex by 1.
Youâll probably then find that itâs skipping the first question. To fix that, in Start, simply set questionIndex = -1 before calling ShowQuestion.
The rest of your code should be OK; it will show the questions in random order, and exit after the last question (which is now the right thing to do), or when the player is out of lives.
Yeah, I want all my questions to be displayed only not in the same order. But because of the random script, sometimes the last question appears second or fifth, etc. and if appears early, the game ends too.
Yeah, since you run out of questions, what you want to do is not to just move to the next question in line. Where you do questionIndex ++. Or you can do that but once questionIndex = questionPool.Length-1 then set it to 0 and start adding to it
It works fantastic!! It shuffles the order and repetition of questions are gone. I am really thankful to you man! By the way, if it is not a burden to you, do you have any idea on where should I put the EndRound()?
As the the game doesnât really end if you answered all the questions correctly, or if you still have lives. It just give an error of IndexOutOfRangeException. What happens is, you have to wait for the last questionâs time to end in order for it to move to the round end display. But again, really thankful to you as you solved my 2 day old problem. Kudos to you my friend!
it doesnât end or bring you to the round end display if you answered all the questions, but if you wait for the given time to end, it moves to the round end display. Can I make it move to the round end display as soon as the questions are all answered? by the way, thanks for the help again man.
OK, when you get an exception, double-click the error message in the Console window. That should open the source file and take you right to the line where it occurred.
(If for some reason that doesnât work, if you read the error message carefully, it also tells you where it occurred, as well as how it got there!)