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);
}
}
var randomColor: Color = new Color(Random.value, Random.value, Random.value);
This should work but for some reason it doesn't. It just keeps chaning between the two different colors. It does the fade effect which is good. In the inspector it contains 4 options. The RandomMusic which is set to 0 and the Color Start which is set to blue and the Color End which is set to green and the Duration which is set to one. Does the color variables need to be some specific value in order to work?
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);
}
Thank you! it works perfectly
– usenameYou're welcome :)
– tanoshimiThanks A Ton :) Worked like a charm!
– Sajalsh25