How to fade between multiple colors?

Hey guys,

I have been trying to get the background color for the main camera to change between ten different colors for the last few days, but I can’t get it to work at all. I’m new to coding so I don’t know if I might be missing something important. This is the code I have so far:

using UnityEngine;
using System.Collections;

public class ColorControl2 : MonoBehaviour {
	
	public Color red = new Color(182.0f, 16.0f, 16.0f);
	public Color green = new Color (8.0f, 189.0f, 10.0f);
	public Color blue = new Color (4.0f, 4.0f, 193.0f, 255.0f);
	public Color gray = new Color (128.0f, 128.0f, 128.0f, 255.0f);
	public Color yellow = new Color (255.0f, 235.0f, 4.0f, 255.0f);
	public Color pink = new Color (251.0f, 7.0f, 154.0f, 255.0f);
	public Color purple = new Color (194.0f, 24.0f, 221.0f);
	public Color orange = new Color (238.0f, 128.0f, 37.0f, 255.0f);
	public Color brown = new Color (88.0f, 61.0f, 21.0f, 255.0f);
	public Color white = new Color (255.0f, 255.0f, 255.0f, 255.0f);
	private Color currentColour;

	// Use this for initialization
	void Start () {
		currentColour = red;
	}
	
	// Update is called once per frame
	void Update () {


		if(Camera.main.backgroundColor == green){
			Camera.main.backgroundColor = Color.Lerp(currentColour, green, 0.1f);
			currentColour = green;
		}
		
		if(Camera.main.backgroundColor == blue){
			Camera.main.backgroundColor = Color.Lerp(currentColour, blue, 0.1f);
			currentColour = blue;
		}    
		
		if(Camera.main.backgroundColor == gray){
			Camera.main.backgroundColor = Color.Lerp(currentColour, gray, 0.1f);
			currentColour = gray;
		}

		if(Camera.main.backgroundColor == yellow) {
			Camera.main.backgroundColor = Color.Lerp(currentColour, yellow, 0.1f);
			currentColour = yellow;
		}

		if(Camera.main.backgroundColor == pink) {
			Camera.main.backgroundColor = Color.Lerp(currentColour, pink, 0.1f);
			currentColour = pink;
		}

		if(Camera.main.backgroundColor == purple) {
			Camera.main.backgroundColor = Color.Lerp(currentColour, purple, 0.1f);
			currentColour = purple;
		}

		if(Camera.main.backgroundColor == orange) {
			Camera.main.backgroundColor = Color.Lerp(currentColour, orange, 0.1f);
			currentColour = orange;
		}

		if(Camera.main.backgroundColor == brown) {
			Camera.main.backgroundColor = Color.Lerp(currentColour, brown, 0.1f);
			currentColour = brown;
		}

		if(Camera.main.backgroundColor == white) {
			Camera.main.backgroundColor = Color.Lerp(currentColour, white, 0.1f);
			currentColour = white;
		}
	}
}

Any ideas what I might be doing wrong?

What you have written there will always be set back to currentColor instantly after being changed by 0.1.
Also the currentColor will never change.

What you want to do is Lerp your current color instead, then set the camera to you currentColor.

This is sudo-code, I’ve not tested it, but you get the idea.

: Edit : Fixed and tested, this should work fine. Lerp is a bad idea though for what you want to achive as it will move towards the given Vector at a relative pace i.e the further away from the target the faster it moves. Causing a rapid jump towards a color then a slow shift before the next big jump.

public Color[] m_Colors;
private Color currentColour;
private int colorIndex = 0;

void Start()
{
    if (m_Colors.Length > 0)
    {
        currentColour = m_Colors[0];
    }
}

void Update()
{
    for (int i = 0; i < m_Colors.Length; i++)
    {
        // Get the currentColor in the Array
        if (currentColour == m_Colors*)*

{
colorIndex = i + 1 == m_Colors.Length ? 0 : i + 1;
}
}
Color nextColor = m_Colors[colorIndex];
// Lerp Color _>
currentColour = Color.Lerp(currentColour, nextColor, 0.025f);
Camera.main.backgroundColor = currentColour;
}

Actually your code is wrong, your assigning the color on the start red but in your if statements there’s nothing checking if its red also once you change the color from inside the if statement it just loops the same color over and over again. Maybe you want to check if the color is not the same like:

if(Camera.main.backgroundColor != white){}

also you can use short cuts for the basic colors in your variables like :

public Color red = Color.red;
public Color blue = Color.blue;

See Unity Color Ref

try this:

using UnityEngine;
using System.Collections;

public class ColorFader : MonoBehaviour 
{
    [System.Serializable]
    public class ColorOptions
    {
        [SerializeField] internal Color color1 = Color.red;
        [SerializeField] internal Color color2 = Color.blue;
        [SerializeField] internal Color color3 = Color.yellow;
        [SerializeField] internal Color color4 = Color.white;
    }
    public enum ColorSelector { Default,Color1,Color2,Color3,Color4}
    public ColorSelector colorSelector;
    [SerializeField] private bool startFade = false;
    public float fadeTime = 0.001f;
    public ColorOptions colorOptions;

    private Color startColor;
    

	// Use this for initialization
	void Start () 
    {
        colorSelector = ColorSelector.Default;
        startColor = Camera.main.backgroundColor; 
	}
	
	// Update is called once per frame
	void Update () 
    {
        if (startFade)
        {
            if (colorSelector == ColorSelector.Default) Camera.main.backgroundColor = Color.Lerp(Camera.main.backgroundColor, startColor, Time.time * fadeTime);
            if (colorSelector == ColorSelector.Color1) Camera.main.backgroundColor = Color.Lerp(Camera.main.backgroundColor, colorOptions.color1, Time.time * fadeTime);
            if (colorSelector == ColorSelector.Color2) Camera.main.backgroundColor = Color.Lerp(Camera.main.backgroundColor, colorOptions.color2, Time.time * fadeTime);
            if (colorSelector == ColorSelector.Color3) Camera.main.backgroundColor = Color.Lerp(Camera.main.backgroundColor, colorOptions.color3, Time.time * fadeTime);
            if (colorSelector == ColorSelector.Color4) Camera.main.backgroundColor = Color.Lerp(Camera.main.backgroundColor, colorOptions.color4, Time.time * fadeTime);
        }
	}
}