- Home /
Randomly changing Background color
Hello I need help, im developing a 2D Platformer game ATM, and i tried many scripts and my own scripts to make a smooth, color transitioning background.. But no script worked, neither the few scripts that are in this forum. All are giving me errors like Internal compiler error or say that "there has to be a semicolon at the end", i did that but then the whole script went crazy.. I dont have an example now because i erased all of them in my rage :v
well.. heres one example from this forum, that doesnt work for me, but should do what i want:
var bgColor : Color;
var blooping : boolean = true;
function Start () {
StartCoroutine(bgColorShifter());
}
function bgColorShifter()
{
while (blooping)
{
bgColor.r = Random.value; // value is already between 0 and 1
bgColor.g = Random.value;
bgColor.b = Random.value;
bgColor.a = 1.0; // I don't think alpha matters
Debug.Log("bgColor: "+bgColor);
var t: float = 0f
var currentColor = Camera.main.backgroundColor;
while( t < 1.0 )
{
Camera.main.backgroundColor = Color.Lerp(currentColor, bgColor, t );
yield null; // Wait one frame
t += Time.deltaTime;
}
}
}
Answer by robertbu · Oct 08, 2013 at 05:20 AM
You are right in that fixing the ';' results in an internal compiler error. The issue is line 23: 'yield null;' It should be just 'yield'. But that kind of mistake should not causes an internal compiler error. Here is the code. I made a few largely cosmetic changes:
#pragma strict
function Start () {
bgColorShifter();
}
function bgColorShifter()
{
var bgColor : Color;
while (true)
{
bgColor = Color(Random.value, Random.value, Random.value, 1.0);
var t: float = 0f;
var currentColor = Camera.main.backgroundColor;
while( t < 1.0 )
{
Camera.main.backgroundColor = Color.Lerp(currentColor, bgColor, t );
yield;
t += Time.deltaTime;
}
}
}
Answer by drudiverse · Jun 20, 2014 at 08:00 AM
heres a script to change the background via HSV controls
hsv is nice version of random color, based on natural colors.
#pragma strict
var Hcol :float;
var Scol :float;
var Vcol :float;
//var SPercent :float;
//var VPercent :float;
function Start () {
// Hcol = Random.value;
//Scol = Random.value ;
//Vcol = Random.value + VPercent / 100 ;
Camera.main.backgroundColor = HSVtoRGB(Hcol,Scol,Vcol);
}
function Update () {
Camera.main.backgroundColor = HSVtoRGB(Hcol,Scol,Vcol);
print (HSVtoRGB(Hcol,Scol,Vcol));
print (HSVtoRGB(Hcol,Scol,Vcol));
}
function HSVtoRGB(h : float, s : float, v : float) {
h=h%1;
s=s%1;
v=v%1;
var r : float;
var g : float;
var b : float;
var i : float;
var f : float;
var p : float;
var q : float;
var t : float;
i = Mathf.Floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v; g = t; b = p; break;
case 1: r = q; g = v; b = p; break;
case 2: r = p; g = v; b = t; break;
case 3: r = p; g = q; b = v; break;
case 4: r = t; g = p; b = v; break;
case 5: r = v; g = p; b = q; break;
}
return Color(r,g,b);
}
Answer by aksh2143 · Aug 21, 2015 at 11:39 AM
i got it.
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
@aksh2143 use time delta and it property best in coroutine or update