I am trying to implement random entry from dictionary function regarded here into unity3d in visual studio : Random entry from dictionary .
private void somefunction() {
Dictionary<string, Sprite> dict = (Dictionary<string, Sprite>) RandomValues(slotImages).Take(5);
foreach (KeyValuePair<string, Sprite> keyValue in dict)
{
Debug.Log("random slotImages name : " + keyValue.Key);
}
}
public IEnumerable<TValue> RandomValues<TKey, TValue>(IDictionary<TKey, TValue> dict)
{
System.Random rand = new System.Random();
List<TValue> values = Enumerable.ToList(dict.Values);
int size = dict.Count;
while (true)
{
yield return values[rand.Next(size)];
}
}
But I am having following error ;
InvalidCastException: cannot cast from source type to destination type.