Hey there Unity, having some strange troubles with my color lerping.

The SlerpToBlack() function runs as expected, but as soon as it gets done and hits color.black, everything pops back to white, and it all stops.

Maybe I’m going about this the wrong way? I’ve commented out other color sources, but they are affected the same way:

public class DayAndNight : MonoBehaviour {
	public Color directionalLightStarting;
	//public Color fogStarting;
	//private Color ambientLightColor;
	private Color directionalLightColor;
	//private Color fogColor;
	//public Color ambientLightStarting;
	public Light mainDirectionalLight;
	public bool slerpToBlack = true;
	public bool slerpToWhite = false;
	public float speed = 0.05f;
	// Use this for initialization
	void Start () {
		mainDirectionalLight = GetComponent<Light> ();
	}
	
	void Update(){
		if (slerpToBlack) {
					SlerpToBlack ();
				}
		if (slerpToWhite) {
					SlerpToWhite();
				}
	}
	public void SlerpToBlack(){
		//ambientLightColor = Color.Lerp (ambientLightStarting, Color.black, Time.time * speed);
		//fogColor = Color.Lerp (fogStarting, Color.black, Time.time * speed);
		directionalLightColor = Color.Lerp (directionalLightStarting, Color.black, Time.time * speed);
		mainDirectionalLight.color = directionalLightColor;
		//RenderSettings.ambientLight = ambientLightColor;
		//RenderSettings.fogColor = fogColor;
		if (mainDirectionalLight.color == Color.black) {
			slerpToBlack = false;
			slerpToWhite = true;
				}
	}
	public void SlerpToWhite(){
		//ambientLightColor = Color.Lerp (Color.black, ambientLightStarting, Time.time * speed);
		//fogColor = Color.Lerp (Color.black, fogStarting, Time.time * speed);
		directionalLightColor = Color.Lerp (Color.black, directionalLightStarting, Time.time * speed);
		mainDirectionalLight.color = directionalLightColor;
		//RenderSettings.ambientLight = ambientLightColor;
		//RenderSettings.fogColor = fogColor;
		if (mainDirectionalLight.color == directionalLightStarting) {
			slerpToWhite = false;
			slerpToBlack = true;
		}
		}
}

Because of what Lerp does, as soon as Time.time * speed equals or exceeds 1, you start doing both SlerpToBlack() and SlerpToWhite() every time in Update.

After the third parameter in Color.Lerp becomes 1, it always returns the target color. That causes you to go inside the if (mainDirectionalLight.color == Color.black) in SlerpToBlack() and because you have 2 if’s instead of an if-else in your Update() you go straight into SlerpToWhite() in the same Update cycle, and again (still) the third parameter is 1 so you go inside the if() in both methods all the time.

This for example should fix it

public class DayAndNight : MonoBehaviour {
public Color directionalLightStarting;
//public Color fogStarting;
//private Color ambientLightColor;
private Color directionalLightColor;
//private Color fogColor;
//public Color ambientLightStarting;
public Light mainDirectionalLight;
public bool nightIsComing = true;
public bool slerpToWhite = false;
public float speed = 0.3f;
public float lightTimer;

public float nightDuration = 2f;
public float nightTimer = 0f;
// Use this for initialization
void Start () 
{
	mainDirectionalLight = GetComponent<Light>();
}

void Update()
{
	if (nightTimer > 0)
	{
		nightTimer -= Time.deltaTime;
	}
	else
	{
		lightTimer += Time.deltaTime * speed;
		if (nightIsComing)
		{
			SlerpToBlack ();
		}
		else
		{
			SlerpToWhite();
		}
	}
}

public void SlerpToBlack(){
	//ambientLightColor = Color.Lerp (ambientLightStarting, Color.black, Time.time * speed);
	//fogColor = Color.Lerp (fogStarting, Color.black, Time.time * speed);
	directionalLightColor = Color.Lerp (directionalLightStarting, Color.black, lightTimer * speed);
	mainDirectionalLight.color = directionalLightColor;
	//RenderSettings.ambientLight = ambientLightColor;
	//RenderSettings.fogColor = fogColor;
	if (mainDirectionalLight.color == Color.black) {
		nightIsComing = !nightIsComing;
		nightTimer = nightDuration;
		lightTimer = 0;
	}
}
public void SlerpToWhite(){
	//ambientLightColor = Color.Lerp (Color.black, ambientLightStarting, Time.time * speed);
	//fogColor = Color.Lerp (Color.black, fogStarting, Time.time * speed);
	directionalLightColor = Color.Lerp (Color.black, directionalLightStarting, lightTimer * speed);
	mainDirectionalLight.color = directionalLightColor;
	//RenderSettings.ambientLight = ambientLightColor;
	//RenderSettings.fogColor = fogColor;
	if (mainDirectionalLight.color == directionalLightStarting) {
		nightIsComing = !nightIsComing;
		lightTimer = 0;
	}
}

}