How can I get different color from the current one every time

I have 4 colors. I want to make it so that the player can’t be the same color 2 times in a row.


using UnityEngine;

public class ColorManager : MonoBehaviour {

	public SpriteRenderer player;
	public string playerColor;

	public Color[] colors = new Color[4];


	private void Awake()
	{
		RandomColor();		
	}

	public void RandomColor()
	{
		int index = Random.Range(0, 4);

		switch (index)
		{
			case 0:
				player.color = colors[0]; //colors[0] is orange
				playerColor = "orange";
				break;

			case 1:
				player.color = colors[1]; //colors[1] is pink
				playerColor = "pink";
				break;

			case 2:
				player.color = colors[2]; //colors[2] is blue
				playerColor = "blue";
				break;

			case 3:
				player.color = colors[3]; //colors[3] is purple
				playerColor = "purple";
				break;
			}    
	}    
}

Any ideas?

Use a while loop to generate your range. Each time you generate the random number, check it against the current, if its not the same, change your While condition to true.

Like he said with the while loop, check to see if say player.color != RandomColor() if true change it to that color