Switch between random colors

I would like to make my background change between different random colors. The script I have right now is this. This script only changes between two colors which are not random.

#pragma strict

var RandomMusic : int;
var colorStart : Color;
	var colorEnd : Color;
	var duration : float = 1.0;
	function Update () {
		var lerp : float = Mathf.PingPong (Time.time, duration) / duration;
		renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);
	}
	if (RandomMusic < 8); {
	    colorStart = Color.cyan; 
	    colorEnd = Color.black; 
	} 
	if (RandomMusic  < 8); {
	    colorStart = Color.red;
	    colorEnd = Color.green; 
	}

The reason why the previous answers are only ever switching between the same two random colours is because they are only ever assigning colorStart and colorEnd once, in the Start() function. Try this instead:

#pragma strict
var colourStart: Color;
var colourEnd: Color;
var rate: float = 1; // Number of times per second new colour is chosen
var i : float = 0; // Counter to control lerp
     
function Start() {
 	colourStart = new Color(Random.value, Random.value, Random.value);
	colourEnd = new Color(Random.value, Random.value, Random.value);
}
     
function Update () {

	// Blend towards the current target colour
  	i += Time.deltaTime*rate;
	renderer.material.color = Color.Lerp (colourStart, colourEnd, i);
	
	// If we've got to the current target colour, choose a new one
	if(i >= 1) {
		i = 0;
		colourStart = renderer.material.color;
		colourEnd = new Color(Random.value, Random.value, Random.value);
	}
}

You can get a random color between two specified colors by:

var randColor =  Color.Lerp (colorStart, colorEnd, Random.value);

Your code above has a number of issues, and I don’t think you want to change the color every frame. Concentrating on only the color change you can do:

#pragma strict
 
var colorStart : Color;
var colorEnd : Color;
var duration : float = 1.0;

function ChangeColor() {
	renderer.material.color =  Color.Lerp (colorStart, colorEnd, Random.value);
}

function Start() {
	InvokeRepeating("ChangeColor", 0.0, duration);
}

Make sure you have the alpha for colorStart and colorEnd set to 255.

Random.value can help you do this:

var randomColor: Color = new Color(Random.value, Random.value, Random.value);

i got it.

using System.Collections;

public class Camera_cript : MonoBehaviour {
Color bgcolor;
Color current;
public Camera camera1;

public void Start ()
{
InvokeRepeating (“Change_color”, 0.0f, 5.0f);
}

void Change_color()
{
	current = new Color (Random.value, Random.value, Random.value);
	bgcolor = new Color (Random.value, Random.value, Random.value);
	camera1.backgroundColor = Color.Lerp (current, bgcolor, 5.0f);

}

}

Thats it and it’ll change every 5 seconds