- Home /
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;
}
Answer by tanoshimi · Dec 22, 2013 at 08:34 PM
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);
}
}
Answer by KellyThomas · Dec 14, 2013 at 04:48 PM
Random.value can help you do this:
var randomColor: Color = new Color(Random.value, Random.value, Random.value);
So why doesn't this work? sorry for the late response. It just changes back and forward from the same colors. I tried the script from the first answer to my question and that works but it changes between the colors in a weird way. The way it changes this script is the way I want it to change. It needs to have that fade effect ins$$anonymous$$d of just fast chaning to another color. How can I do this?
#pragma strict
var Random$$anonymous$$usic : int;
var colorStart : Color = new Color(Random.value, Random.value, Random.value);
var colorEnd : Color = new Color(Random.value, Random.value, Random.value);
var duration : float = 1.0;
function Update () {
var lerp : float = $$anonymous$$athf.PingPong (Time.time, duration) / duration;
renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);
}
Try this:
#pragma strict
var Random$$anonymous$$usic: int;
var colorStart: Color;
var colorEnd: Color;
var duration: float = 1.0;
function Start() {
colorStart = new Color(Random.value, Random.value, Random.value);
colorEnd = new Color(Random.value, Random.value, Random.value);
}
function Update () {
var lerp: float = $$anonymous$$athf.PingPong (Time.time, duration) / duration;
renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);
}
I suspect the way unity uses the inspector to initialise public variables might be messing things up for you.
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 Random$$anonymous$$usic 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?
Oh, you want an endless series or random colours?
var colorStart: Color;
var colorEnd: Color;
var colorTransitionTime: float = 10.0;
var colorElapsedTime: float = 0.0;
function randomColor (): Color {
return new Color(Random.value, Random.value, Random.value);
}
function Start() {
colorStart = randomColor();
colorEnd = randomColor();
}
function Update () {
colorElapsedTime += Time.deltaTime;
while (colorElapsedTime > colorTransitionTime) {
colorStart = colorEnd;
colorEnd = randomColor();
colorElapsedTime -= colorTransitionTime;
}
renderer.material.color = Color.Lerp (colorStart, colorEnd, colorElapsedTime / colorTransitionTime);
}
Answer by robertbu · Dec 14, 2013 at 04:46 PM
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.
Answer by aksh2143 · Aug 21, 2015 at 11:39 AM
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
Your answer
Follow this Question
Related Questions
C# Randomize Background.Color Issues 1 Answer
weird color or lighting bug in Unity? 1 Answer
How to change color of sprite as it moves over different backgrounds 2 Answers
Change Scene Color : How to acess color correction curves from a c# script 0 Answers
Why colors are being mixed after I try to change them? 1 Answer