- Home /
Display random element from dictionary
Hi everyone, I need your help I want to pick up random element from dictionary and display this element’s key and valeu in different UI Texts. Until now I figured up just how to display random value. Thanks for your help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine.UI;
public class ListManager : MonoBehaviour {
// public string car;
//public int speed;
public Text keyText;
public Text valueText;
Dictionary<string, int> database = new Dictionary<string, int>();
// Use this for initialization
void Start () {
AddToDatabase();
ShowRandomElement();
}
// Update is called once per frame
void Update () {
}
public void ShowRandomElement()
{
var randomKey = database.Keys.ToArray()[(int)Random.Range(0, database.Keys.Count - 1)];
var randomValueFromDictionary = database[randomKey];
valueText.text = randomValueFromDictionary.ToString();
foreach (KeyValuePair<string,int> item in database)
{
Debug.Log ("Key: "+item.Key+","+"Item: "+item.Value);
}
}
public void AddToDatabase()
{
database.Add("Lamborghini Veneno", 350);
database.Add("Bughatti Veyron", 420);
database.Add("Porchce Carera", 400);
database.Add("Hennesey GT", 460);
database.Add("MCLaren P1", 370);
database.Add("MCLaren F1", 430);
}
Thanks for this post. I didn't know about the ToArray() function.
Answer by RodrigoAbreu · Jun 26, 2020 at 04:48 AM
Hey @Sottnik, what is up!? So, I'm guessing you're wanting to have a different new UI text element for each of the new values you're randomizing.
So:
Remove those text elements from this class
Create a new class (also Monobehaviour) holding only those two elements, call it maybe something like TextWidget or something like that.
Expose some method to pass those two strings, maybe a setter where you'll pass those 2 strings and then in that setter you'll just set those strings into the Text.text.
Create a new object in your hierarchy.
Add that new TextWidget (Or whatever you call it) to that new object.
Drag this object to your Project window to create a prefab out off it.
Now in your ListManager class add an entry, a new reference to that widget type, and pass the prefab to it through inspector.
In your method "ShowRandomElement", now instantiate (or use an object pool if you really need more in runtime) and pass those strings to that object's setter as I said above.
Remember to set your instance of "TextWidget" to have the proper parent (The correct canvas where you intend to display those elements) textWidgetInstance.transform.SetParent(PASS_YOUR_CANVAS_TRANSFORM_HERE)
And I guess that's it.
Tip: Avoid iterating through hash structures like dictionaries, they tend to get slower for iteration as it grows in elements. If you need to keep growing that dictionary, please consider creating some logic with a dictionary and a list, maybe a list of your keys (strings) and then you'd be able to randomize one of those keys and then use that key to tryGet the value from the dictionary. That way you'd just need to randomize an int considering the size of that list, then use a list[index] to retrieve the string key, use that string key to tryGetting your value and you're good.