
Shuffle your lists so they are randomised. Can be used once per game or each time your list is called..
public static class ListX
{
/// <summary>
/// Shuffles a list using Unity's Random
/// </summary>
/// <typeparam name="T">The data type</typeparam>
/// <param name="_list">The list to shuffle</param>
/// <returns>The list in a shuffled order</returns>
static public List<T> ShuffleList<T>(List<T> _list)
{
for (int i = 0; i < _list.Count; i++)
{
T temp = _list[i];
int randomIndex = UnityEngine.Random.Range(i, _list.Count);
_list[i] = _list[randomIndex];
_list[randomIndex] = temp;
}
return _list;
}
}
You have a deck and you want it to be in a random order:
You have a set of names and you want to be in a random order:
private List<string> playerNames;
private void Start()
{
playerNames.Add("Brendan");
playerNames.Add("Alfred");
playerNames.Add("Jessica");
ShuffleList(playerNames);
}
Put this into your ListX.cs script, and call anywhere using ListX.Shuffle