Scripting randomised buttons and text fields for a quiz game

I’m working on a new quiz game, which will be my second quiz-type application. In the first app I made, I had more than 100 scenes that each had a canvas with a text field (question) and 4 buttons (answer options). 3 buttons were obviously the wrong answer and 1 button was the correct answer and the OnClick events were setup to run a WrongAnswer or RightAnswer method. This whole setup works well and creating new questions is relatively hassle-free, however there is some manual work to be done in setting up a scene, positioning the buttons, updating the text fields etc.

In the new app, I’d like to use 1 scene if I can, with a text field for the question, and 4 buttons, but I would like to somehow keep a list of different questions along with associated answers and script it to update the text. I think I could work out how to pull the question and update the text, but not sure how Id do the buttons with the OnClick events. Does anyone have any ideas, or has done something like this previously? Keen to save myself all those hours I’ll need to create scenes if I had to do this manually…

You can definitely use just one scene. I did something similar awhile back, though it was in ActionScript. Same principles apply though:

–Make a 2D String[ ] array, 100 x 4: 100 (or however many questions), 4 answers in each, have the first one always be the right answer.

–When pulling the questions out, make a temp copy of the 4-element array and draw from it randomly, removing each element as you do. Track when you grab index 0 (the correct one) so you know which number it is on the screen, and therefore whether the user clicks the right one. Because the answer is always index 0, the reshuffling of the indices as you draw from the array won’t matter. (Likely there are other ways to do this too.)

For the UI, you’re reusing 1 text area (the question) and 4 buttons (the answers), just changing the text each time. When you set up the OnClick() or OnPointerDown() event for the buttons, use a function which takes an integer argument telling your script which one was clicked (either 0-3 or 1-4, whichever makes sense to you). With an argument in the function, there’s a field in the Inspector where you can set the argument. That way you know which button was clicked on any question. Hope that helps!

Thanks Seejay for helping me to get my thinking process rolling. I’d say my own skills are a bit beyond beginner and maybe intermediate level, but some of this is going to be new to me! Will get cracking and see what I can come up with. Thank you!

1 Like

You’re very welcome!
Start with an array of 3 or 4 questions and the 2D array of corresponding answers, see if you can do the “random drawing” part to assign to the buttons, get the button arguments set, and determine if the right/wrong logic works. That’s most of it right there.

1 Like