How do I make the elements of my array random?

Ive got an array with 3 colours and I want them to be randomly assigned to game objects spawning in but not sure how to do this. Any help would be amazing!



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

public class Main : MonoBehaviour {

public static float Range;

public GameObject[] squaresDownArray;
public GameObject[] squaresUpArray;
public GameObject square;

int index;

public Color[] colourArray;

public Vector2 leftSpawnPosition;
public Vector2 midSpawnPosition;
public Vector2 rightSpawnPosition;

public float spawnTimer;
private float spawnCounter;

private int squareIndexCounter;

// Use this for initialization
void Start () {

	//squaresDownArray = GameObject.FindGameObjectsWithTag ("SquareDown");
	//squaresUpArray = GameObject.FindGameObjectsWithTag ("SquareUp");

	InvokeRepeating ("TimerTick", 0, 1);


// Update is called once per frame
void Update () {
	spawnCounter += Time.deltaTime;


		public void TimerTick(){
	if(spawnCounter >= spawnTimer){
	GameObject lSquare = Instantiate (square, leftSpawnPosition, Quaternion.identity);
	lSquare.tag = "SquareDown";
		//lSquare.GetComponent<SpriteRenderer> ().color = colourArray [squareIndexCounter];
		lSquare.GetComponent<MeshRenderer>().materials[0].color = colourArray [squareIndexCounter];

		if (squareIndexCounter >= colourArray.Length) {
			squareIndexCounter = 0;

	GameObject mSquare = Instantiate (square, midSpawnPosition, Quaternion.identity);
	mSquare.tag = "SquareUp";
		//mSquare.GetComponent<SpriteRenderer> ().color = colourArray [squareIndexCounter];
		mSquare.GetComponent<MeshRenderer>().materials[0].color = colourArray [squareIndexCounter];
		if (squareIndexCounter >= colourArray.Length) {
			squareIndexCounter = 0;

	GameObject rSquare = Instantiate (square, rightSpawnPosition, Quaternion.identity);
	rSquare.tag = "SquareDown";
		//rSquare.GetComponent<SpriteRenderer> ().color = colourArray [squareIndexCounter];
		rSquare.GetComponent<MeshRenderer>().materials[0].color = colourArray [squareIndexCounter];

		if (squareIndexCounter >= colourArray.Length) {
			squareIndexCounter = 0;
		spawnCounter -= spawnTimer;

	foreach (GameObject square in GameObject.FindGameObjectsWithTag("SquareUp")) {
		square.GetComponent<SquareScript> ().Move ("up");

	foreach (GameObject square in GameObject.FindGameObjectsWithTag("SquareDown")) {
		square.GetComponent<SquareScript> ().Move ("down");





here is the color i believe you are looking for :


Well as mentioned above you just need to use Random.Range().

However to it’s always better to have a clean code so If I were you I would have created a method to do that job. Remember every method must execute ONLY what it’s name implies.

public Color AssignRandomColor(){
         return ColorArray[Random.Range(0,colourArray.length)] ; 

Then whenever you want to get a random color you can just call this method and a random color will be returned.


mSquare.GetComponent<MeshRenderer>().materials[0].color = AssignRandomColor();

Hope this helps!!!