How can I make an object change colours randomly?

Unity virgin here. I have followed a tutorial on YouTube on creating a audio visualiser. Now it looks good, but how can you make it so that the colour changes every couple of seconds?
Here’s the code I used:

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

public class Spectrum : MonoBehaviour {

// Use this for initialization
public GameObject prefab;
public int numberOfObjects = 20;
public float radius = 5f;
public GameObject[] cubes;
public Material Mat;
public GameObject cameraPivot;

void Start() {
	
	for (int i = 0; i < numberOfObjects; i++) {
		float angle = i * Mathf.PI * 2 / numberOfObjects;
		Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
		Instantiate(prefab, pos, Quaternion.identity);

	}
	cubes = GameObject.FindGameObjectsWithTag ("cubes");

}

// Update is called once per frame
void Update ()
{
	
	float[] Spectrum = AudioListener.GetSpectrumData (1024, 0, FFTWindow.Hamming);
	for (int i = 0; i < numberOfObjects; i++) {
		Vector3 previousScale = cubes *.transform.localScale;*

previousScale.y = Mathf.Lerp (previousScale.y, Spectrum * 40, Time.deltaTime * 30);
_ cubes .transform.localScale = previousScale;
* }
cameraPivot.transform.Rotate (0, 0.5F, 0);
}
}*
Thanks in Advance :slight_smile:_

Instead of using a Coroutine you can simply use a timer that resets perhaps every 2 seconds. Also note that if you instantiate objects you do not need to Find them again since Instantiate returns the object it creates, so you can simply cache that in an array. The Random.ColorHSV has a lot of parameters that can be clamped so that it will not pick very dark and desaturated colors.

using UnityEngine;

public class Spectrum : MonoBehaviour
{
    // Public
    public GameObject cubePrefab;
    public int numberOfObjects = 20;
    public float radius = 5f;
    public float changeColorDelay = 2f;
    public GameObject cameraPivot;
    // Private
    private Renderer[] cubesRenderers;
    private float timer;

    // Awake is called when the script instance is being loaded
    void Awake()
    {
        cubesRenderers = new Renderer[numberOfObjects];
        GameObject obj;
        for (int i = 0; i < numberOfObjects; i++)
        {
            float angle = i * Mathf.PI * 2 / numberOfObjects;

            Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;

            obj = Instantiate(cubePrefab, pos, Quaternion.identity);
            cubesRenderers *= obj.GetComponent<Renderer>();*

}
}

// Update is called once per frame
void Update()
{
float[] Spectrum = AudioListener.GetSpectrumData(1024, 0, FFTWindow.Hamming);
for (int i = 0; i < numberOfObjects; i++)
{
Vector3 previousScale = cubesRenderers*.transform.localScale;*
previousScale.y = Mathf.Lerp(previousScale.y, Spectrum * 40, Time.deltaTime * 30);
cubesRenderers*.transform.localScale = previousScale;*
}
cameraPivot.transform.Rotate(0, 0.5F, 0);

DelayedRandomMaterialColor();
}

void DelayedRandomMaterialColor()
{
timer += Time.deltaTime;
if (timer > changeColorDelay)
{
// Random Color 100% saturated and above 50% brightness(value)
var randomColor = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
RendererColor(cubesRenderers, randomColor);
timer = 0f;
}
}

void RendererColor(Renderer[] renderers, Color color)
{
for (int i = 0; i < renderers.Length; i++)
{
cubesRenderers*.material.color = color;*
}
}

}

You can generate a random color with a function like this

private Color GenerateRandomColor()
{
    return randomColor = new Vector4
    (
        Random.Range(0f,1f),    //red
        Random.Range(0f,1f),    //green
        Random.Range(0f,1f),    //blue
        1f,                                      //transparency
    )
}

and then you can assign that to a material every couple seconds pretty easily with

private IEnumerator SetColorAfterDelay()
{
    yield return new WaitForSeconds(2f); //or however many seconds
    Mat.color = GenerateRandomColor();

    StartCoroutine(SetColorAfterDelay());
}


private void Start()
{
    StartCoroutine(SetColorAfterDelay());
}