- Home /
multiple choice question in Unity
Hi I'm new to Unity, and have very limite programming skills. I want to make a question window, in the window it will have one question, the question will have four different answers. If the use select the wrong answer, "Try angain" will appear; if the use select the right answer, "Well done" will appear, and a close button will show up.
Can anyone give me some ideas? Thanks very much
===========================================
EDIT: Hi Bravini thank you very much
Sorry,I only can understand very simple code, so I tried to modified your code, but the last part not working, could you point out what's wrong? Thanks very much
var textToShow1: String; var textToShow2: String; var textToShow3: String; var textToShow4: String;
function OnGUI () {
GUI.Label (Rect (20, 10, 500, 30), "-------------Q1?-------------------");
GUI.Label (Rect (120, 40, 500, 30), textToShow1); GUI.Label (Rect (120, 70, 500, 30), textToShow2); GUI.Label (Rect (120, 100, 500, 30), textToShow3); GUI.Label (Rect (120, 130, 500, 30), textToShow4);
if(GUI.Button(Rect(20,40,80,20),"A1")) { textToShow1="Try Again"; } if(GUI.Button(Rect(20,70,80,20),"A2")) { textToShow2="Try Again"; } if(GUI.Button(Rect(20,100,80,20),"A3")) { textToShow3="Try Again"; }
if (GUI.Button(Rect(20,130,80,20),"A4")){
textToShow4 ="Well done!";
if(GUI.Button(Rect(20,160,80,20),"close")){
Application.LoadLevel (1);
}
}
}
=======================================================
EDIT: I've fixed the problem. Thanks! At least I've learned something =)
var isQuestionaryOn = false;
function OnGUI(){ if (GUI.Button(Rect(20,130,80,20),"A4")){ isQuestionaryOn=true; textToShow4 ="Well done!"; }
if (isQuestionaryOn){ if(GUI.Button(Rect(20,160,80,20),"Back")){ Application.LoadLevel(1); } }
}
I'm a little puzzled as to why you selected as the accepted answer an incomplete code example that doesn't even do what you asked for...? Anyway, the code I posted earlier does exactly what you asked for, is complete and self-contained, and has been successfully tested. If the reason you weren't able to get what you needed from it is that it's written in C#, please say so (as that is a problem that's easily solved).
Hi Jesse, Thanks for comments. As you can see my coding level is very basic. I know your code is very advance but I can't understand it, if you could write in JS and make it very simple, I will be very appreciate. Thank you
I added a simplified version in UnityScript (aka JS) to my post. That's about as simple as I can make it and still have it do what you asked, I think, but if you have any questions about it, feel free to ask.
Answer by Bravini · Nov 14, 2010 at 10:57 PM
there are many ways to do this, a simple though not very elegant wqay is to nest GUI.buttons. a snippet for you to arrange your commands would be(in js) :
var textToShow: String;
<p>function OnGUI () {</p>
<blockquote>
<p>GUI.Label (Rect (10, 10, 100, 20), textToShow);
if (isQuestionaryOn) {//your boolean variable that makes the questionary appear
if(GUI.Button(Rect(10,10,50,50),"wrongButton")) { //wrong button is a text, name it like the answer you want</p>
<blockquote>
<p>textToShow="Try Again";</p>
</blockquote>
<p>}</p>
</blockquote>
<p>if (GUI.Button(Rect(10,10,50,50),"RighButton")){</p>
<blockquote>
<blockquote>
<p>textToShow ="Well done!";</p>
<p>if(GUI.Button(Rect(10,10,50,50),"close")){</p>
<blockquote>
<blockquote>
<p>yield new WaitForSeconds(5);
Application.Quit(); //this will close the game</p>
</blockquote>
</blockquote>
<p>}</p>
</blockquote>
</blockquote>
<p>}</p>
<p>}</p>
this could also be done with GUI.BeginGroup or with GUITexture objects to use like buttons, if you want something fancier.
http://unity3d.com/support/documentation/ScriptReference/GUI.BeginGroup.html
http://unity3d.com/support/documentation/Components/gui-Controls.html
Answer by Jesse Anders · Nov 14, 2010 at 10:17 PM
The most straightforward solution would probably be to use OnGUI() and GUI controls for this.
For the question itself, you can use the 'label' control. For the answers, you could use (e.g.) the 'selection grid' control or a set of 'button' controls. For actions such as 'try again' or 'close', you can use the 'button' control. Keep track of the current state ('question', 'try again', or 'close'), and create appropriate controls in your OnGUI() function depending on the current state. Then, switch between states as appropriate based on user actions.
Unless I've missed something, that should cover what you described in your post.
Edit: I usually try to avoid giving complete code examples, but that hasn't been working out for me so well lately, so here's some example code (C#). No guarantee of correctness, but I've tested it and it seems to work as expected. Formatting, layout, etc. are up to you, but this implements the basic functionality that I described above:
using UnityEngine;
public class MultipleChoice : MonoBehaviour { public string question; public string[] answers; public int correctAnswer; // Note that this is a zero-based index
enum State { Question, TryAgain, Close };
State state = State.Question;
void OnGUI()
{
switch (state) {
case State.Question:
DoQuestion();
break;
case State.TryAgain:
DoTryAgain();
break;
case State.Close:
DoClose();
break;
}
}
void DoQuestion()
{
GUILayout.Label(question);
for (int i = 0; i < answers.Length; ++i) {
if (GUILayout.Button(answers[i])) {
if (i == correctAnswer) {
state = State.Close;
} else {
state = State.TryAgain;
}
}
}
}
void DoTryAgain()
{
GUILayout.Label("Wrong answer.");
if (GUILayout.Button("Press to try again...")) {
state = State.Question;
}
}
void DoClose()
{
GUILayout.Label("Correct!");
if (GUILayout.Button("Close")) {
// Whatever you want to do here (e.g. call Application.Quit()).
}
}
}
Edit: Adding a simplified version in UnityScript, as requested. Please note that I don't use UnityScript myself, and as such I can't guarantee that the following code follows all UnityScript 'best practices'. However, it does compile and run, and behaves the same as the C# version as far as I can tell.
Here's the code:
var question : String; var answers : String[]; var correctAnswer : int;
enum State { Question, TryAgain, Close };
private var state : State = State.Question;
function OnGUI() { if (state == State.Question) { GUILayout.Label(question); for (i = 0; i < answers.Length; ++i) { if (GUILayout.Button(answers[i])) { if (i == correctAnswer) { state = State.Close; } else { state = State.TryAgain; } } } } else if (state == State.TryAgain) { if (GUILayout.Button("Wrong answer. Click this button to try again...")) { state = State.Question; } } else if (state == State.Close) { if (GUILayout.Button("Correct! Click this button to close...")) { Application.Quit(); } } }
Hi Jesse, Thanks! I tried but nothing shows on screen, where shall I change? Sorry the code is too hard for me. Or could you tell me what's wrong with my simple code?
The code should work as is. Did you create a .js script and attach it to an object in your scene? Also, did you assign values to the 'question', 'answers', and 'correctAnswer' fields?
Yes, I created a .js and attached it to $$anonymous$$ain Camera. Also, I assigned value to "question", but I don't know how to assign values to "answers" and "correctAnswer". Could you tell me
If you assigned a value to 'question' but you're still not seeing anything, then something else must be wrong with your setup. In any case, to assign values to 'answers', expand the field by clicking on the disclosure triangle, and you should see a field labeled 'Size'. For this field, enter the number of answers you want, after which you should see a list containing that number of elements. Assign the answers to these elements. For the 'correct answer' field, simply enter the zero-based index of the correct answer. I'm out of characters, but post back if you have further questions.
Hi Jesse Thanks so much for your help and patience. I got it.
Your answer
Follow this Question
Related Questions
Text pop up when mouse over gui button 2 Answers
Overlapping GUI Button priority 3 Answers
Button on Panel is not responding 1 Answer
Texture2d Array with Next Button 1 Answer
popup message with a button 0 Answers