- Home /
Changing camera background color in runtime
Hello!
I tried to make a simple scene and change the camera's background color.
My script:
using UnityEngine; using System.Collections;
public class MainMenuItem : MonoBehaviour { private bool newGame; public Color color1; public Color color2; public float duration;
private Camera cam;
void Awake()
{
cam = Camera.mainCamera.camera;
}
void Start()
{
newGame = false;
color1 = Color.black;
color2 = Color.white;
duration = 3.0f;
}
void Update()
{
if(newGame)
{
float t = Mathf.PingPong(Time.time, duration) / duration;
cam.backgroundColor = Color.Lerp(color1, color2, t); //line 33 NullRef...
if(cam.backgroundColor == Color.white)
{
Application.LoadLevel("TestScene1");
}
}
}
void OnMouseEnter()
{
renderer.material.color = Color.red;
}
void OnMouseExit()
{
renderer.material.color = Color.white;
}
void OnMouseDown()
{
newGame = true;
}
}
I got NullReferenceException in line 33. I don't now how to continue. How can I get the camera and change the parameters?
Answer by Zyklon · Dec 05, 2011 at 05:30 PM
Finally, it's working! I had some problems with the Color.Lerp, but thats also working now.
The script on the camera:
using UnityEngine;
using System.Collections;
public class CameraChangeColor : MonoBehaviour
{
public bool click = false;
public Color color1 = Color.black;
public Color color2 = Color.white;
public float duration = 3.0f;
private float deltaTime = 0.0f;
void Start ()
{
color1 = Color.black;
color2 = Color.white;
duration = 3.0f;
deltaTime = 0.0f;
}
void Update ()
{
if(click)
{
deltaTime += Time.deltaTime;
if(deltaTime < duration)
{
camera.backgroundColor = Color.Lerp(color1, color2, deltaTime);
}
else
{
Application.LoadLevel("TestScene1");
}
}
}
}
... and on the menu text:
using UnityEngine;
using System.Collections;
public class MainMenuGUINewGame : MonoBehaviour
{
public GameObject go;
public CameraChangeColor other;
void Awake()
{
go = GameObject.Find("Camera");
other = (CameraChangeColor) go.GetComponent(typeof(CameraChangeColor));
}
void OnMouseEnter()
{
renderer.material.color = Color.red;
}
void OnMouseExit()
{
renderer.material.color = Color.white;
}
void OnMouseDown()
{
other.click = true;
}
}
It's looks nice. :D
Answer by aldonaletto · Dec 05, 2011 at 12:55 PM
Replace the instruction in Awake:
void Awake() { cam = Camera.main; }I suspect Camera.mainCamera doesn't even exist! Anyway, the main camera must be tagged MainCamera (what it is by default).
Answer by drudiverse · Jun 20, 2014 at 07:59 AM
heres a script to change the background via HSV controls
#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);
}