Get Random Item - OuterRimStudios/Utilities GitHub Wiki
CollectionUtilities.GetRandomItem
public static T GetRandomItem<T>(this T[] array)
array The array of objects to choose from.
T a random element from the entered array.
Get a random element of a given array.
using UnityEngine;
using OuterRimStudios.Utilities;
public class ExampleClass : MonoBehaviour
{
string[] names = new string[] {"Sam", "Hector", "Jeff"};
void Start()
{
string randName = CollectionUtilites.GetRandomItem(names); //Returns a random name from the array
}
}
public static T GetRandomItem<T>(this List<T> list)
list The list of objects to choose from.
T a random element from the entered list.
Get a random element of a given list.
using UnityEngine;
using OuterRimStudios.Utilities;
public class ExampleClass : MonoBehaviour
{
List<string> names = new List<string>() {"Sam", "Hector", "Jeff"};
void Start()
{
string randName = CollectionUtilites.GetRandomItem(names); //Returns a random name from the array
}
}