Color GUILayout Buttons from Array of Colors (C#)

Hi everyone, I’m having a bit of an issue and right when I thought I figured it out I have a problem. I’m making a game that allows you to pick from 30 randomly-colored icons.

Here is an array of colors I’ve created:

public Color[] avatarColors = new Color[9];

void Start () {

		avatarColors [0] = new Color (255/255f, 130/255f, 171/255f); //palevioletred1
		avatarColors [1] = new Color (218/255f, 112/255f, 214/255f); //orchid
		avatarColors [2] = new Color (131/255f, 111/255f, 255/255f); //slateblue1
		avatarColors [3] = new Color (100/255f, 149/255f, 237/255f); //cornflowerblue
		avatarColors [4] = new Color (174/255f, 238/255f, 238/255f); //paleturquoise2
		avatarColors [5] = new Color (152/255f, 251/255f, 152/255f); //palegreen
		avatarColors [6] = new Color (154/255f, 205/255f, 50/255f); //olivedrab3
		avatarColors [7] = new Color (240/255f, 230/255f, 140/255f); //khaki
		avatarColors [8] = new Color (255/255f, 193/255f, 37/255f); //goldenrod1

	}

Now, making my GUILayout use these colors should be simple, here is my attempt:

for (int i = 0; i < 3; i++) {
			GUILayout.BeginHorizontal ();
			GUILayout.FlexibleSpace ();
			for (int j = 0; j < 10; j++){
				GUI.contentColor = avatarColors[Random.Range (0, avatarColors.Length)];
				if(GUILayout.Button (avatarImage, faceplateStyle, GUILayout.Width (avatarWidth), GUILayout.Height(avatarHeight))){}
				GUI.contentColor = preColor;
			}
			GUILayout.FlexibleSpace ();
			GUILayout.EndHorizontal ();
			GUILayout.Space (bufferSpace);
		}

This successfully creates three rows of ten icons (30 items total) with random colors; however, the colors fluctuate constantly! I’m not sure how to assign the colors in a way that is both completely random, and that doesn’t change once it’s been set.

Thank you in advance to anyone who can help!

Randomize the colors in the array first, rather than randomly picking colors from it every frame. By the way, use Color32 instead of Color, then you don’t need to do math.

I fixed my problem, here is the result:

public Color[] avatarColors = new Color[10];

	void Start () {

		//There are ten potential pastel colors for coloring avatars, one for each digit 0-9.
		avatarColors [0] = new Color (255/255f, 130/255f, 171/255f); //palevioletred1
		avatarColors [1] = new Color (218/255f, 112/255f, 214/255f); //orchid
		avatarColors [2] = new Color (131/255f, 111/255f, 255/255f); //slateblue1
		avatarColors [3] = new Color (100/255f, 149/255f, 237/255f); //cornflowerblue
		avatarColors [4] = new Color (174/255f, 238/255f, 238/255f); //paleturquoise2
		avatarColors [5] = new Color (152/255f, 251/255f, 152/255f); //palegreen
		avatarColors [6] = new Color (154/255f, 205/255f, 50/255f); //olivedrab3
		avatarColors [7] = new Color (255/255f, 246/255f, 143/255f); //khaki1
		avatarColors [8] = new Color (255/255f, 215/255f, 0/255f); //gold1
		avatarColors [9] = new Color (205/255f, 92/255f, 92/255f); //indianred

		ShuffleArray (avatarColors);

	}

void OnGUI () {

GUILayout.BeginArea (menuBackground);
			
			//This section creates avatar icons in the menu. These icons are not the actual avatars.
			for (int i = 0; i < 3; i++) {
				GUILayout.BeginHorizontal ();
				GUILayout.FlexibleSpace ();
				for (int j = 0; j < 10; j++){
					int k = Mathf.Abs ((i + 9) * (j * 7));
					while(k >= 10)
						k /= 10;
					GUI.contentColor = avatarColors[k];
					if(GUILayout.Button (avatarImage, faceplateStyle, GUILayout.Width (avatarWidth), GUILayout.Height(avatarHeight))){}
					GUI.contentColor = preColor;
				}
				GUILayout.FlexibleSpace ();
				GUILayout.EndHorizontal ();
				GUILayout.Space (bufferSpace);
			}

			GUILayout.EndArea (); }

	//This function can shuffle an array. Call it using ShuffleArray(yourArray);
	public static void ShuffleArray<T>(T[] arr) {
		for (int i = arr.Length - 1; i > 0; i--) {
			int r = Random.Range(0, i);
			T tmp = arr*;*

_ arr = arr[r];_
* arr[r] = tmp;*
* }*
* }*