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
2
Question by Nooch · Dec 17, 2011 at 11:30 PM · guiarraytextclass

Storing questions/answers for a multiple choice game

Hi all,

Noob here :) I was wondering what recommendations you would have about a good way to store a sizable amount of questions (and answers) for a multiple-choice type game.

If it were just 10-15 questions I might hard code them, but if I want to have many more, I imagine there's a better solution.

Maybe in to an array? Could I create some type class to define each question/answer?

Thanks, any help or pointers are appreciated!

Comment
Add comment
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

1 Reply

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

Answer by jahroy · Dec 17, 2011 at 11:54 PM

Here's a very simple example of how you would create a class named Question.

It demonstrates how you would use a for loop to initialize the array full of dummy values.

It then tests the data by printing a bunch of labels with a for loop.

I'm sure there are errors... I just typed it up real quick.

If there are no errors, you should be able to use this script as is.

A more practical approach would be to get rid of the Start function (it's only for an example) and define your questions in the Inspector.

If you add that script to a GameObject, you should see a slot in the Inspector named Array Of Questions. You can populate this by entering a size then typing text into the slots named Question Text and Answer Text for each element in the array.

Hope this helps.

var arrayOfQuestions : Question [];

class Question { var questionText : String; var answerText : String;

 /* example constructor (creates a Question from two params) */

 function Question ( q : String, a : String )
 {
     questionText = q;
     answerText = a;
 }

}

/ initialize the question array in the start function /

function Start () { arrayOfQuestions = new Question [20];

 /* use a for loop to create dummy questions */

 for ( var i = 0; i < 20; i ++ ) {

     var someQ : String = "Question number: " + i;
     var someA : String = "Answer number: " + i;

     /* use the example constructor from above */

     var freshQuestion : Question = new Question(someQ, someA);

     arrayOfQuestions[i] = freshQuestion;
 }

}

/ 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.answerText;

     GUI.Label(labelRect, txt);

     labelRect.y += labelRect.height;
 }

}

Comment
Add comment · Show 7 · 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 Nooch · Dec 18, 2011 at 05:08 AM 1
Share

Wow, this is a HUGE help! Thanks for the explanation. A follow-up question, regarding your suggestion: how would I go about initializing the array with the size and entries that I set in the inspector?

For example, when I use: arrayOfQuestions = new Question [20]; ... it's creating a new array with 20 empty slots in it, as opposed to the number (and entries) that I've specified in the inspector. If I understand that correctly, of course. ;)

avatar image Nooch · Dec 18, 2011 at 05:23 AM 0
Share

Oh wait... it seems that it does already initialize the array, and I can access it directly without having to create a new empty one in the script. Awesome!

avatar image jahroy · Dec 18, 2011 at 05:33 AM 0
Share

I think you understand perfectly.

When you enter a size in the Inspector, some code behind the scenes must call something that looks very much like this:

 exampleTransformArray = new Transform [numberFromInspector];

You $$anonymous$$UST allocate memory for a builtin array before you start to populate or play with it

(unless you let the Inspector do it for you).

There are also Dynamic Arrays available in javascript that you can resize on the fly.

You can play with a dynamic array, get it dialed-in the way you like it, and convert it to a builtin array, too.

A good place to do this is in Editor scripts (where performance doesn't matter very much). That's how you can initialize an array of an unknown size and turn it into a builtin, typed array.

For example, you might have a million questions listed in a text file. You could read them in with an editor script and assign them to an array in a script.

Unity makes this VERY easy with TextAssets:

http://answers.unity3d.com/questions/131720/jahroy-text-asset-toot.html

Boy... Unity is pretty cool.

avatar image mradebe_eti · Jul 16, 2013 at 09:25 PM 0
Share

Hi,

I'm new to Unity. Is there a C# version of the above function? I want to try it out for a multiple choice game.

Any help or guidance is appreciated.

Thanks!

avatar image jahroy · Jul 17, 2013 at 07:03 AM 0
Share

C# and UnityScript are quite similar. If you understand one of them, it should be trivial to convert. If you don't understand one of them, I can't think of a better way to learn than to give it a try. If you rely on others to do something this simple for you, you'll never learn anything.

I'll give you one hint. In UnityScript you declare a Rect like this:

 var someRect : Rect = Rect(200, 200, 100, 100);

The same declaration in C# would look like this:

 Rect someRect = Rect(200, 200, 100, 100);


C# does not require the var keyword and the type of the variable comes before its name (in s$$anonymous$$d of co$$anonymous$$g after it and being separated by a colon).

Just keep your eye on the errors in the console (in red) and do some googling and you'll figure it out real quick. Good luck!

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't destroy an arrray with GUI Text 1 Answer

Move the text in a GUI Button/Box 1 Answer

GUI.label overlapping text 1 Answer

Gui Text Script 4 Answers

Alpha not working in GUITex 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