Problem using Mathf.Lerp to fade a point light up and down

OK so this is probably just me misunderstanding how to use Mathf.Lerp correctly but for some reason instead of my light fading up and down it just seems to jump from one intensity to the next. The code is really simple but for some reason i just cant get it to do what i want it to. if someone wouldn’t mind looking at it for me to tell me what im doing wrong i would very grateful.

using UnityEngine;
using System.Collections;

public class SeasonsAndWeather : MonoBehaviour {
	
	public Light mainLight;
	public float seasonTimer;
	private int iSeason;
	private string[] season = {
		"spring",
		"spring/summer",
		"summer",
		"summer/autumn",
		"autumn",
		"autumn/winter",
		"winter",
		"winter/spring"
	};
	public float fadeTime = 30.0f;
	private string currentSeason;
	public string CurrentSeason
	{
		get{return currentSeason;}
		set{currentSeason = value;}
	}
	private bool seasonsActive;
	public bool SeasonsActive
	{
		get{return seasonsActive;}
		set{seasonsActive = value;}
	}
	//0=spring 1=spring/summer 2=summer 3=summer/autumn 
	//4=autumn 5=autumn/winter 6=winter 7=winter/spring
	// Use this for initialization
	void Start () {
		iSeason = 0;
		currentSeason = season[iSeason];
		seasonsActive = true;
		seasonTimer=5.0f;
	}
	// Update is called once per frame
	void Update () {
		if(seasonsActive)
		{
			seasonTimer -= Time.deltaTime;
			if (seasonTimer <=0.0f)
			{
				if (iSeason == season.Length-1)
				{
					iSeason = 0;
					currentSeason = season[iSeason];
					Debug.Log (season[iSeason]);
					seasonTimer=5.0f;
				}
				else
				{
					iSeason++;
					currentSeason = season[iSeason];
					Debug.Log (season[iSeason]);
					seasonTimer=30.0f;
					if (currentSeason == "spring/summer")
					{
						mainLight.light.intensity = Mathf.Lerp(3.5f, 4.5f, fadeTime*Time.time);
					}
					if (currentSeason == "summer/autumn")
					{
						mainLight.light.intensity = Mathf.Lerp(4.5f, 3.5f, fadeTime*Time.time);
					}
					if (currentSeason == "autumn/winter")
					{
						mainLight.light.intensity = Mathf.Lerp(3.5f, 2.5f, fadeTime*Time.time);
					}
					if (currentSeason == "winter/spring")
					{
						mainLight.light.intensity = Mathf.Lerp(2.5f, 3.5f, fadeTime*Time.time);
					}
				}
			}
		}	
	}
}

Many thanks for any help that you could give me

Decalre a varible called lerpTime or similar after the fade time and then instead of multiplying the Time.time or Time.deltaTime, add Time.deltaTime/totalTime every frame. so that you would get what you want. Further, there have been few logical flaws in the code, which I have tried to clean

    using UnityEngine;
    using System.Collections;
     
    public class SeasonsAndWeather : MonoBehaviour {
     
        public Light mainLight;
        public float seasonTimer;
        private int iSeason;
        private string[] season = {
           "spring",
           "spring/summer",
           "summer",
           "summer/autumn",
           "autumn",
           "autumn/winter",
           "winter",
           "winter/spring"
        };
        public float fadeTime = 30.0f;
        public float lerpTimer = 0f;          //Declared by Flamy
        private string currentSeason;
        public string CurrentSeason
        {
           get{return currentSeason;}
           set{currentSeason = value;}
        }
        private bool seasonsActive;
        public bool SeasonsActive
        {
           get{return seasonsActive;}
           set{seasonsActive = value;}
        }
        //0=spring 1=spring/summer 2=summer 3=summer/autumn 
        //4=autumn 5=autumn/winter 6=winter 7=winter/spring
        // Use this for initialization
        void Start () {
           iSeason = 0;
           currentSeason = season[iSeason];
           seasonsActive = true;
           seasonTimer=5.0f;
        }
        // Update is called once per frame
        void Update () {
           if(seasonsActive)
           {
              /*
             seasonTimer -= Time.deltaTime;       
              what is season timer for? if it is for 
               changing the season after 30 sec.. then no use remove it!!
               the current logic does that
             if (seasonTimer <=0.0f)
             {
               The whole of this logic is not needed to be here, so im am moving it below
              if (iSeason == season.Length-1)
              {
                  iSeason = 0;
                  currentSeason = season[iSeason];
                  Debug.Log (season[iSeason]);
                  seasonTimer=5.0f;
              }
              else
              */
              {
                 // I am moving this logic to a place where it might work.
                 // iSeason++;
                 // currentSeason = season[iSeason];
                 // Debug.Log (season[iSeason]);
                 // seasonTimer=30.0f;        

                  lerpTime += (Time.deltaTime/fadeTime);

                  if (currentSeason == "spring/summer")
                  {
                     mainLight.light.intensity = Mathf.Lerp(3.5f, 4.5f, lerpTime);
                  }
                  else if (currentSeason == "summer/autumn")
                  {
                     mainLight.light.intensity = Mathf.Lerp(4.5f, 3.5f, lerpTime);
                  }
                  else if (currentSeason == "autumn/winter")
                  {
                     mainLight.light.intensity = Mathf.Lerp(3.5f, 2.5f, lerpTime);
                  }
                  else if (currentSeason == "winter/spring")
                  {
                     mainLight.light.intensity = Mathf.Lerp(2.5f, 3.5f, lerpTime);
                  }
                  if(lerpTime>=1)
                  {
                     iSeason++;
                     if (iSeason >= season.Length)
                     {
                          iSeason = 0;
                     }
                     lerpTimer = 0f;
                     currentSeason = season[iSeason];
                     Debug.Log (season[iSeason]);
                  }
              }
             }
           } 
        }

The below is the modified clean code for the fade

using UnityEngine;
using System.Collections;
 
public class SeasonsAndWeather : MonoBehaviour {
 
    public Light mainLight;
    public float seasonTimer;
    private int iSeason;
    private string[] season = {
       "spring",
       "spring/summer",
       "summer",
       "summer/autumn",
       "autumn",
       "autumn/winter",
       "winter",
       "winter/spring"
    };
    public float fadeTime = 30.0f;
    public float lerpTimer = 0f;          //Declared by Flamy
    private string currentSeason;
    public string CurrentSeason
    {
       get{return currentSeason;}
       set{currentSeason = value;}
    }
    private bool seasonsActive;
    public bool SeasonsActive
    {
       get{return seasonsActive;}
       set{seasonsActive = value;}
    }
    //0=spring 1=spring/summer 2=summer 3=summer/autumn 
    //4=autumn 5=autumn/winter 6=winter 7=winter/spring
    // Use this for initialization
    void Start () {
       iSeason = 0;
       currentSeason = season[iSeason];
       seasonsActive = true;
       
    }
    // Update is called once per frame
    void Update () {
       if(seasonsActive)
       {
          
          lerpTime += (Time.deltaTime/fadeTime);

          if (currentSeason == "spring/summer")
          {
             mainLight.light.intensity = Mathf.Lerp(3.5f, 4.5f, lerpTime);
          }
          else if (currentSeason == "summer/autumn")
          {
             mainLight.light.intensity = Mathf.Lerp(4.5f, 3.5f, lerpTime);
          }
          else if (currentSeason == "autumn/winter")
          {
             mainLight.light.intensity = Mathf.Lerp(3.5f, 2.5f, lerpTime);
          }
          else if (currentSeason == "winter/spring")
          {
             mainLight.light.intensity = Mathf.Lerp(2.5f, 3.5f, lerpTime);
          }
		
          if(lerpTime>=1)
          {
             iSeason++;
             if (iSeason >= season.Length)
             {
                  iSeason = 0;
             }
			 lerpTimer =0;
             currentSeason = season[iSeason];
             Debug.Log (season[iSeason]);
          }
	    }
    }
} 

This should work fine I guess, though it is untested…

Hope it helps :slight_smile:

Use Time.deltaTime instead of Time.time in the Mathf.Lerp function