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