Hey guys, Im making a board game which involves multiple choice questions to move forward. this is the script which displays the questions:
var arrayOfQuestions : Question ;
class Question
{
var questionText : String;
var answerText1 : String;
var answerText2 : String;
var answerText3 : String;
var answerText4 : String;
var correctAnswer : String;
/* example constructor (creates a Question from two params) */
function Question ( q : String, a : String, b : String, c : String, d : String, correct : String )
{
questionText = q;
answerText1 = a;
answerText2 =b;
answerText3 =c;
answerText4=d;
correctAnswer = correct;
}
}
/* initialize the question array in the start function */
function Start ()
{
arrayOfQuestions = new Question [2];
/* use a for loop to create dummy questions */
arrayOfQuestions[0] = new Question("Where are we?", "E6", "K8", "G1", "MoCap", "K8");
arrayOfQuestions[1] = new Question("Where are we?", "E6", "K8", "G1", "MoCap", "K8");
}
/* test it */
function OnGUI ()
{
var labelRect : Rect = Rect(200, 200, 300, 30);
for ( var thisQ : Question in arrayOfQuestions ) {
var txt = "Q: " + thisQ.questionText + " A: " + thisQ.answerText1 + " B: " + thisQ.answerText2 + " C: " + thisQ.answerText3 + " D:" + thisQ.answerText4;
GUI.Label(labelRect, txt);
labelRect.y += labelRect.height;
}
}
i was just wondering how do i link a button press to it?
Putting the text on buttons as @Nanobrain suggests is one good approach. No matter how you do it, I recommend restructuring your possible answers into an array as @Nanobrain has in his example code. Another approach is to make your label text clickable. As with the buttons, that means you are going to have to display each possible answer individually rather than combining all the Q/A into a single string. Here is a bit of code to demonstrate how to make the text clickable. I uses Rect.Contains() to detect a mouse click within the Rect used to display the text. In a new scene, attach the script to an empty game object and run:
#pragma strict
private var question = "Where does he live?";
private var possibleAnswers = ["Paris", "London", "New York", "Mexico City"];
private var labels = [" A: ", " B: ", " C: ", " D: "];
private var questionRect = Rect(200,200,400,30);
private var answerRects = [Rect(200, 235, 400, 25), Rect(200, 270, 400, 25), Rect(200, 305, 400, 25), Rect(200, 340, 400, 25)];
function OnGUI() {
var e = Event.current;
GUI.Label(questionRect, question);
if (e.type == EventType.Repaint) {
for (var i = 0; i < possibleAnswers.Length; i++) {
GUI.Label(answerRects<em>, labels_+possibleAnswers*); *_</em>
* }* * }*
* // Check the mouse click* * if (e.type == EventType.MouseDown) {* * for (i = 0; i < possibleAnswers.Length; i++) {* _ if (answerRects*.Contains(e.mousePosition)) { Debug.Log(“The user clicked on answer #” + i); break; } } } }*_
There’s a few ways this could be done, depending on what results you’re wanting. One way would be to add your answer texts each as the text for a button.
Button(answerButtonRect, answerText1);
And so you could position the buttons wherever you wish, however your layout determines.
To detect a click on the button place it in an IF statement. If the result equals ‘true’ then the button was pressed that frame.
if(new Button(answerButtonRect, answerText1)) {
// place code here to execute when answer 1 is pressed
}
That said, your answers should be placed in an array as well, so that you can iterate through them and place a button on each one of them more easily and with less, more readable, sustainable code.
// please mind any syntax errors, I code using C#
for(var index:int = 0; index < answerText.length; index ++) {
if(new Button(answerButtonRect, answerText[index])) {
// execute code for selection # index
// maybe a switch statment? Up to you.
}
}
shall i do the same lay out as i did for the array of questions, like this:
var arrayOfQuestions : Question ;
var arrayOfAnswers: Answers ;
class Question
{
var questionText : String;
var answerText1 : String;
var answerText2 : String;
var answerText3 : String;
var answerText4 : String;
function Question ( q : String, a : String, b : String, c : String, d : String)
{
questionText = q;
answerText1 = a;
answerText2 =b;
answerText3 =c;
answerText4=d;
}
}
class Answers
{
var answerText1 : String;
var answerText2 : String;
var answerText3 : String;
var answerText4 : String;
var correctAnswer : String;
function Answers (a : String, b : String, c : String, d : String, correct : String )
{
answerText1 = a;
answerText2 =b;
answerText3 =c;
answerText4=d;
correctAnswer = correct;
}
}