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 Alanimator · Oct 16, 2013 at 12:43 AM · c#guidictionaryquiz

Creating Q&A Quiz game using Dictionaries as the database

I am making a quiz game and I have decided to use Dictionaries to store Q&A. I am making some progress but I have some issues which I will now outline .

you just need to attach this script to a gameobject .

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;

 public class testQuestions : MonoBehaviour {

I start be declaring some global variables and creating the dictionary and adding content to the dictionary in my start function .

 Dictionary<string, string[]> dictionary = new Dictionary<string, string[]>();
     string []vl ;
     string ky;
     //int Qnum;
     string A;
     int indx;
     // Use this for initialization
     void Start () {
         dictionary.Add("ups", new string[] {"updegree", "popup"});
         dictionary.Add("down", new string[] {"sun", "bun"});
         dictionary.Add("left", new string[] {"higi", "migi"});
         dictionary.Add("right", new string[] {"een", "yyo"});
     }
     

I then use up my update function to check if a button is pressed and to run a function which generates a random key from the dictionary

 // Update is called once per frame
         void Update ()
         {
             if (Input.GetKeyDown("q"))
             {
                 GenerateRandoms();
             }
         }

then i create the function where a random key is selected

 strng[] keys = new string[dictionary.Count];//get the dictionary count and store in array
 dictionary.Keys.CopyTo(keys, 0);
  
 var index = Random.Range(0, keys.Length);
 var key = keys[index];
 var value = dictionary[key];
         ky= key;
         vl = value;
         
 foreach (string ku in value)
 {
             
             // create buttons with the ku text as the button text 
             
             
             indx = index;
             //Qnum +=1;    
             A = ku; 
     
         }
         //---------- remove each after answer button click
         //dictionary.Remove(key); // remove key after it is already displayed. this must only be exicuted at the end of the function
         
     }

then i create the GUI function where i should have buttons dynamically created to match the number of values in the randomly selected key with each button text being a valur from that dictionary key

 void OnGUI ()
     {
         // here  dynamically added butons must be added 
         GUI.TextArea(new Rect (0,0,500,20),"Key is " + ky + " value is " + string.Join(",", vl) + " " +int.Parse(indx.ToString()));
         
         if (GUI.Button (new Rect(0, 40, 100, 20),A))
         {
             
             if (dictionary.Keys.ElementAt(dictionary.Count - indx).Contains(A))
             {
                 print ("correct");
             } 
         }
 
         
     }
 }


how can i dynamically create the buttons and just get this thing to work

Comment
Add comment · Show 1
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 floAr · Oct 18, 2013 at 07:52 AM 0
Share

If your problem was solved by the answer provided, please mark it as an answer, if not, feel free to comment on it with problems

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by floAr · Oct 16, 2013 at 11:19 AM

To be honest I am not 100% about your question. If I got everything right you store your Question with possible answers in the Dictionary and now want to draw a procedural button menue. I see a problem there, as you would need another field, indicating which answer is the correct one. Regarding your problem think some of you functions can be simplified to fit into the following approach:

  • On ButtonPress pick a random question

  • If question is picked show answer menue

  • If no question is picket show something else


       using UnityEngine;
         using System.Collections;
         using System.Collections.Generic;
         using System.Linq;
         
         public class testQuestions : MonoBehaviour
         {
             //Question array
             Dictionary<string, string[]> dictionary = new Dictionary<string, string[]>();
             int pickedQuestion = -1;
         
             void Start()
             {
                 dictionary.Add("ups", new string[] { "updegree", "popup" });
                 dictionary.Add("down", new string[] { "sun", "bun" });
                 dictionary.Add("left", new string[] { "higi", "migi" });
                 dictionary.Add("right", new string[] { "een", "yyo" });
             }
         
             void Update()
             {
                 if (Input.GetKeyDown("q"))
                 {
                     GenerateRandoms();
                 }
             }
         
             void GenerateRandoms()
             {
                 pickedQuestion = (int)Random.Range(0, dictionary.Count);//pick the index of one question
             }
         
             void OnGUI()
             {
                 if (pickedQuestion != -1)
                 {
                     // here dynamically added butons must be added
                     GUI.TextArea(new Rect(0, 0, 500, 20), "Key is " + dictionary.Keys[pickedQuestion] + " value is " + string.Join(",", dictionary.Values[pickedQuestion]) + " " + pickedQuestion);
                     for (int i = 0; i < dictionary.Values[pickedQuestion].Length; i++)
                     {
                         if (GUI.Button(new Rect(0, 40 * i, 100, 20), dictionary.Values[pickedQuestion][i]))
                         {
                             //check if value is correct
                         }
                     }
                 }
                 else
                 {
     //handle alternative gui here
                 }
     
             }
         }
    
    

Hope this helps you getting started.

PS: I think it would be easier to encapsulate the question into an extra class like this:

 public class Question
 {
  string Text{get;set;}
  string[] Answers{get;set;}
  int CorrectAnswerId{get;set;}
 }

Then you could use a simple ArrayList in you main class and you will get all the information for one question into one single place.

Comment
Add comment · Show 1 · 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 Alanimator · Oct 19, 2013 at 12:32 AM 0
Share

i tried your method but then a bunch of errors got thrown up which shouldn't . i managed to get the whole thing working with just under 100 lines of code :)

this is by far the easiest method of getting a Quiz done :)

the partially complete code

the fix is in my comment

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

16 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Putting Dictionary/List using foreach as buttons in a scroll view? 3 Answers

Putting dictionary items in GUI ScrollView 1 Answer

Distribute terrain in zones 3 Answers

Erroe when resizing GUITextures in C# file 1 Answer


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