Question by
MuffinMan101010 · Aug 10, 2016 at 07:09 PM ·
cameracolorbackground
Help with this Camera Background Color Code
I want to make my camera background color turn red when I press space and five seconds later turn back to blue. It normally was just supposed to get red when I press space and It worked fine, but now I try to revert the color back to blue and It simply doesn't work. Obviously I'm doing something wrong. Here's the code
using UnityEngine;
using System.Collections;
public class CameraBackGroundTest : MonoBehaviour
{
private Color red = Color.red;
private Color blue = Color.blue;
Camera cam;
void Start()
{
cam = GetComponent<Camera>();
}
IEnumerator Update()
{
if (Input.GetKeyUp ("space"))
{
cam.backgroundColor = red;
yield return new WaitForSeconds(5);
cam.backgroundColor = blue;
}
}
}
Thanks for the help!
Comment
Best Answer
Answer by btmedia14 · Aug 10, 2016 at 07:56 PM
@MuffinMan101010 The Update() method is a unity method and has a signature with return type void. Try the following where you detect the space key in the Update() method, but use a coroutine to process the camera color.
void Update()
{
if (Input.GetKeyUp("space"))
{
StartCoroutine(camBackColor());
}
}
private IEnumerator camBackColor()
{
cam.backgroundColor = red;
yield return new WaitForSeconds(5f);
cam.backgroundColor = blue;
}