Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lucy · Nov 14, 2010 at 09:40 PM · buttonpopupmultiple-choice

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); } }

}

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jesse Anders · Nov 15, 2010 at 02:10 AM 0
Share

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).

avatar image Lucy · Nov 15, 2010 at 02:14 AM 0
Share

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

avatar image Jesse Anders · Nov 15, 2010 at 03:19 AM 0
Share

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.

avatar image Lucy · Nov 15, 2010 at 03:22 AM 0
Share

Thanks very much! I have lots of questions :)

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0
Best Answer

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 &lt; 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(); } } }

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lucy · Nov 15, 2010 at 03:47 AM 0
Share

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?

avatar image Jesse Anders · Nov 15, 2010 at 04:15 AM 0
Share

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?

avatar image Lucy · Nov 15, 2010 at 08:03 PM 0
Share

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

avatar image Jesse Anders · Nov 15, 2010 at 11:24 PM 0
Share

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.

avatar image Lucy · Nov 16, 2010 at 01:39 AM 0
Share

Hi Jesse Thanks so much for your help and patience. I got it.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges