Random element from the list

Hi!
I have simple wave spawn system in which the waves(elements in the list) are successively.
But I need a random sequence of waves from the list and I don’t know how do it.

It’s maybe look like

waves [Random.Range(0, waves.Count)];

Please help!

Thanks in advance!

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class WaveAction
{
	public string name;
	public float delay;
    public GameObject[] hazards;
	public int spawnCount;
	public string message;
	public float spawnWait;
	public Vector3 spawnValues;

}

[System.Serializable]
public class Wave
{
	public string name;
	public List<WaveAction> actions;

}



public class WaveGenerator : MonoBehaviour
{
	public float difficultyFactor = 0.9f;
	public List<Wave> waves;
	private Wave m_CurrentWave;
	public Wave CurrentWave { get {return m_CurrentWave;} }
	private float m_DelayFactor = 1.0f;

	void Start()
	{
		StartCoroutine(SpawnLoop());
	}

	IEnumerator SpawnLoop()
	{
		m_DelayFactor = 1.0f;
		while(true)
		{
			foreach(Wave W in waves)
			{
				m_CurrentWave = W;
				foreach(WaveAction A in W.actions)
				
				{
					if(A.delay > 0)
						yield return new WaitForSeconds(A.delay * m_DelayFactor);
					if (A.message != "")
					{
						//messageText.text = A.message;
					}
					if (A.hazards != null && A.spawnCount > 0)
					{
						for(int i = 0; i < A.spawnCount; i++)
						{
							GameObject hazard = A.hazards [Random.Range (0, A.hazards.Length)];
							Vector3 spawnPosition = new Vector3 (Random.Range (-A.spawnValues.x, A.spawnValues.x), A.spawnValues.y, A.spawnValues.z);
							Quaternion spawnRotation = Quaternion.identity;
							Instantiate (hazard, spawnPosition, spawnRotation);
							yield return new WaitForSeconds (A.spawnWait);
						}
				    }
				}
				yield return null;  // prevents crash if all delays are 0
			}
			m_DelayFactor *= difficultyFactor;
			yield return null;  // prevents crash if all delays are 0
        }
	}	
}

Well, i designed this spawning system so you can setup different waves which usually get more difficult later. (Think about WarCraft3 tower defence maps. They often have 100 waves with increasing difficulty). Choosing waves at random in such a case usually doesn’t make sense. However you can of course implement a random order.

If you don’t care that it might pick the same wave twice in a row the line you posted in your question is already enough to pick a random wave.

If it should be an endless game just remove the foreach loop:

 IEnumerator SpawnLoop()
 {
     m_DelayFactor = 1.0f;
     while(true)
     {
         m_CurrentWave = waves [Random.Range(0, waves.Count)];
         foreach(WaveAction A in m_CurrentWave.actions)
         // ...

If you want to first use up all waves before one repeats you would use a temporary List, fill it with all available waves, pick a random one and remove that wave from the temporary list. If the list is empty (all waves have been used) you have to re add all waves from the original list.

If in addition you want to prevent that the same wave is picked twice in a row when this re-adding occurs, you can use 3 temporary lists. You would use one active list (from which you pick your wave) one “used” list and a “buffer” list. When you pick a wave from the active list you remove it from the active list and add it to the buffer list. If the buffer list has more than “5” waves in it, you move the first item from the “buffer” to the “used” list. When the active list is empty you refill it with the waves in the used list. That way it’s not possible to pick one of the last 5 waves since they are still in the buffer. Those waves will be used in the next cycle. I just picked “5” in case there are about 50 waves. So about 10% of the available waves. If you only have 7 waves it makes no sense to buffer 5.