Changing Background Colour by Code

In my camera settings I’ve got a solid colour background, and when I click on the color in the inspector I can see the HSVA settings (H47 S243 V253 A5 is bright yellow for example).
Can anyone please tell me, or point me in the right direction as to how I change these settings during the course of a game?

I know how to turn things on or off, or to change single settings, but all my efforts at changing these HSVA settings have been unfruitful.

Why there is no built-in converter, I have no idea. There’s one for the Editor but not the player.

EDIT: ignore this post, Jessy’s reply is more helpful

Colors in unity are stored in RGB color space. The color editor allows you to specify it using other color spaces, but the data it produces for your camera is RGBA. Since your script has to work with this data, it has to provide colors in RGB space. If you want to be able to use HSV color space from your scripts, you will need to write HSV/RGB conversion functions.

This page the algorithms involved (although you’ll have to be able to read C for it, unfortunately):

Thank you both for pointing me in the right direction. Much appreciated. I’ve looked at both the links and I’ve spent quite a bit of time reading through from there, but everything I tried come up with code errors.

I kept changing things about and then running the script and finally I’ve got no code errors, but nothing seems to happen either!

What I’ve got at present is this

GameObject.Find ("CameraTop");
backgroundColor = Color (0.5, 0.5, 0.5, 1);

Where CameraTop is the name of my camera.

No code errors showing, but not working either (should change color to grey)

Any suggestions.

How does “backgroundColor” Link up with “CameraTop”? Right now the variable “backgroundColor” is just there for the sake of being there, and not doing anything.

I’ve tried adding the following to the top of the script at various times with vatious results.

var camera : GameObject;
var camera = GameObject.Find("CameraTop");

I assumed calling the “Camera Top”

GameObject.Find ("CameraTop");

linked the following line to it

backgroundColor = Color (0.5, 0.5, 0.5, 1);

Presumably this isn’t true, so how do I link the “CameraTop” line to the “backgroundColor” line please?

camera.backgroundColor = …

Appels / Jessy

Thanks for your links. I have looked at the example, but I can’t get it to work on collision and obviously haven’t understood it correctly.

I’m starting with a yellow background, and using the following code which I hoped would change the color to something else.

var color1 = Color.red;
var color2 = Color.blue;
var duration = 3.0;

function OnControllerColliderHit (hit : ControllerColliderHit) {

if (hit.gameObject.tag == "hit02"){
var t = Mathf.PingPong (Time.time, duration) / duration;
camera.backgroundColor = Color.Lerp (color1, color2, t);
}
}

The camera is seperate from my FPC. When I attach the code to my FPC I get the following error message.

When I attach the code to my camera I get no error message but the background color doesn’t change.

When I make the camera a child of my FPC I still get no error message, and the background color doesn’t change (but the camera angle is then all wrong anyway)

I’ve obviously not understood. Could someone please explain where am I going wrong?

you must be doing something wrong, i wrote this quickly and it works just fine :

var color1 = Color.red; 
var color2 = Color.blue; 
var duration = 3.0; 

function changeColor()
{ 
	var t = Mathf.PingPong (Time.time, duration) / duration; 
	camera.backgroundColor = Color.Lerp (color1, color2, t);
}


function OnGUI()
{
	if(GUI.Button(Rect(10,10,100,20), "changeColor"))
	{
		changeColor();
	}
}

I attached this to my camera and click the button.

Appels,

I’m begginning to feel extremely thick (actually, not beginning, I’ve felt thick for ages!). It must be something to do with the ‘collision’ part of the script, but I can’t work pout how to link the things together.

When I replace your mouse down part with my FPC collision part as follows

var color1 = Color.red;
var color2 = Color.blue;
var duration = 3.0;

function changeColor()
{   
var t = Mathf.PingPong (Time.time, duration) / duration;
   camera.backgroundColor = Color.Lerp (color1, color2, t);
}

function OnControllerColliderHit (hit : ControllerColliderHit) {
if (hit.gameObject.tag == "hit02")
 {     
changeColor();	
}
}

I don’t get an error message if I attach it to the camera, but when I collide with “hit2” nothing happens.

I’d actually spent quite a while looking at the examples on the links, but I can’t seem to tie them into a collision script.

Still can’t work out how to do this, so I am obviously doing something wrong but I can’t work out what.

Thanks for all the suggestions so far, they are all really appreciated.

ok but do you get it to work using my way, just to make sure that part is working on your side.

i just made it work like you want it to work with the collider :

var color1 = Color.red; 
var color2 = Color.blue; 
var duration = 3.0; 

function changeColor() 
{    
   var t = Mathf.PingPong (Time.time, duration) / duration; 
   camera.main.backgroundColor = Color.Lerp (color1, color2, t); 
} 

function OnControllerColliderHit (hit : ControllerColliderHit) { 
   if (hit.gameObject.tag == "hit02") 
   {      
      changeColor();    
   } 
}

and make sure your object your colliding with has the tag assigned to it.
also note the the camera.main naming.

Appels,

Sorry, but I still can’t get your code to work for me.

I even created a completely new scene with only a terrain, FPC with attached ‘Main Camera’ FPSWalker Script, rigid Body and Character Controller. I’ve cleared the flags on the camera setting to ‘solid Color’ and set that color to yellow.

I’ve then added a cube with a tag of “hit02” with a Box Collider and a Rigid Body.

I’ve attached your following code to the ‘Main Camera’ and I’ve used the code with

and

I’ve also tried changing the name of ‘Main Camera’ to ‘Camera Main’

When I run the programme, the sky color still doesn’t change when my FPC collides with the “hit02” tagged cube.

var color1 = Color.red; 
var color2 = Color.blue; 
var duration = 3.0; 

function changeColor() 
{    
   var t = Mathf.PingPong (Time.time, duration) / duration; 
   camera.main.backgroundColor = Color.Lerp (color1, color2, t); 
} 

function OnControllerColliderHit (hit : ControllerColliderHit) { 
   if (hit.gameObject.tag == "hit02") 
   {      
      changeColor();    
   } 
}

Help! I’m quickly going insane!

add me on messenger, appels@rocketmail.com

OK, for those coming later, here’s the solution.

Appels could get the code to work for him, but I couldn’t get it to work for me.

Appels sent me his package and I ran it on a laptop (where his code worked for me) alongside my desktop (whetre his code didn’t work for me!

I couldn’t see any difference between the two packages, but when I deleted the camera on the desktop and then re-created a camera with identical settings it then worked.

So I’ve still no idea why the code didn’t work originally, but the following code now works on collision

var cam;
var camColor;
var color1 = Color.red;
var color2 = Color.blue;
var duration = 3.0;

function changeColor()
{
   var t = Mathf.PingPong (Time.time, duration) / duration;
   cam = GameObject.Find("Camera");
   cam.GetComponent(Camera).backgroundColor = Color.Lerp (color1, color2, t);
}

function OnControllerColliderHit (hit : ControllerColliderHit) {
if (hit.gameObject.tag == "hit02")
   {
      changeColor();
}
}

My gratefull thanks to everyone for their help, especially Appels who spent a few hours helping.

Thanks

the ‘var camColor’ isn’t used anymore.
you can remove that line.