Day and Night Cycle Not Working

So I followed a how-to video on how to implement a day and night cycle. However, once I enter “play” mode, nothing happens. There aren’t any compilation errors and I set-up the universal pipeline renderer asset as instructed by the video. Attached is a screen shot of how I have the game object “Global Light 2D” set-up and also the C# script.

Day-and-night

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;


public class DayAndNightCycle : MonoBehaviour
{
    [SerializeField] private Gradient lightColor;
    [SerializeField] private Light2D light;

    private int days;

    public int Days => days;

    private float time = 50;

    private bool canChangeDay = true;

    private void Update()
    {
        if(time > 500)
        {
            time = 0;
        }

        if((int)time == 250 && canChangeDay)
        {
            canChangeDay = false;
            days++;
        }

        if ((int)time == 255)
            canChangeDay = true;

        time += Time.deltaTime;
        light.GetComponent<Light2D>().color = lightColor.Evaluate(time * 0.002f);
   
    }
}

Script

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

If you are running a mobile device you can also see the console output. Google for how.