Stuck with unity quiz game logic
I am trying to creeate a quiz where the user is shown 8 or 6 Images along side a larger image panel the objective is for the user to guess the correct rule for sequence and click the box corresponding to the answer.
Currently each game object for the image has a renderer attached for the image to change when a new question is loaded and i have a reference to the panel to display a larger picture as the question I can currently change the buttons and the image using values stored from an array for each
The bit i am struggling with currently is how change the question randomly when a correct answer is chosen. I currently have set up classes with each values to change the images of everything to a new question but i cannot figure out how to loop through these and setup a random set of images for the questions
example class for a single question: private void practice1() {
//Qbtns = sprite renderer array-quesBtn is a list of images for the buttons/ or answers Qbtns[0].sprite = quesBtn[0]; Qbtns[1].sprite = quesBtn[1]; Qbtns[2].sprite = quesBtn[2]; Qbtns[3].sprite = quesBtn[3]; Qbtns[4].sprite = quesBtn[4]; Qbtns[5].sprite = quesBtn[5]; Qbtns[6].sprite = quesBtn[6]; Qbtns[7].sprite = quesBtn[7]; panel.GetComponent<Image>().sprite = queS[0]; }
I was thinking something like a switch statement to go through these but i am stuck with logic of how to loop thorugh these
switch (Questions1[Questions1.Count]) { case 1: practice1(); break; case 2: practice2(); break; case 3: test1(); break;
I attempted to make this as clear as possible but i understand its a bit long winded, thanks to anyone who takes the time to read/answer! :D
Answer by tormentoarmagedoom · May 16, 2019 at 08:05 AM
Hello dino.
If you pretend to find a random element in an array, you will need to use Random.Range()
And then, if pretend to no repeat one element, to recieve always a new one, you should have a "availability array". I mean, have a boolean array, so each question stores in this array when is used.
So you should have some logic like this:
bool [] QuationsUsed = new bool[number of questions]
int number = random.range
while ( QuationsUsed[number] = true)
{
number = random.range
}
At this point, numbner will be only a non-taken question/number. So now, lets use it.
You chose Questions[number] , and set its bool as true to prevent beeig selected again
QuationsUsed [number] = true;
Thats all you need!
Bye!
thanks for this answer , i will try it out and let you know
Your answer
Follow this Question
Related Questions
How to make bullet auto find target unity 2d game? 0 Answers
After I built my game the exe opens and closes immediately !!! 0 Answers
How to put images in questions of a quiz , where did the database through String [ ] ? 0 Answers
High Score not working - Quiz Game 0 Answers
How do I modify my code to not repeat the questions? 0 Answers