I’m working on a youtube tutorial for creating a day/night cycle and I’ve run into an error.
the error is:
NullReferenceException: Object reference not set to an instance of an object
GameTime.SetUpLighting () (at Assets/DayNight-Package/Scripts/GameTime.cs:115)
GameTime.Start () (at Assets/DayNight-Package/Scripts/GameTime.cs:58)
the code is:
using UnityEngine;
using System.Collections;
public class GameTime : MonoBehaviour
{
public enum TimeOfDay
{
Idle,
SunRise,
SunSet,
}
public Transform[] sun;
public float dayCycleInMinutes = .5f;
public float Idle = 0;
public float SunRise = .11f;
public float SunSet = .6f;
public float skyboxBlendModifier = 5;
private Sun[] _sunScript;
private float _degreeRotation;
private float _timeOfDay;
private float _dayCycleInSeconds;
private const float SECOND = 1;
private const float MINUTE = 60 * SECOND;
private const float HOUR = 60 * MINUTE;
private const float DAY = 24 * HOUR;
private const float DEGREES_PER_SECOND = 360 / DAY;
private TimeOfDay _tod;
private float _noonTime;
private float _morningLength;
private float _eveningLength;
void Start ()
{
_tod = TimeOfDay.Idle;
_dayCycleInSeconds = dayCycleInMinutes * MINUTE;
RenderSettings.skybox.SetFloat("_Blend",0);
_sunScript = new Sun[sun.Length];
for(int cnt = 0; cnt < sun.Length; cnt++)
{
Sun temp = sun[cnt].GetComponent<Sun>();
if (temp = null)
{
Debug.LogWarning("Sun script not found. Adding it.");
sun[cnt].gameObject.AddComponent<Sun>();
temp = sun[cnt].GetComponent<Sun>();
}
_sunScript[cnt] = temp;
}
_timeOfDay = 0;
_degreeRotation = DEGREES_PER_SECOND * DAY / (_dayCycleInSeconds);
SunRise *= _dayCycleInSeconds;
SunSet *= _dayCycleInSeconds;
_noonTime = _dayCycleInSeconds / 2;
_morningLength = _noonTime - SunRise;
_eveningLength = SunSet - _noonTime;
SetUpLighting ();
}
void Update ()
{
_timeOfDay += Time.deltaTime;
if (_timeOfDay > _dayCycleInSeconds)
{
_timeOfDay -= _dayCycleInSeconds;
}
for (int cnt = 0; cnt < sun.Length; cnt++) {
sun [0].Rotate (new Vector3 (_degreeRotation, 0, 0) * Time.deltaTime);
}
if (_timeOfDay > SunRise _timeOfDay < _noonTime)
{
AdjustLighting(true);
} else if(_timeOfDay > _noonTime _timeOfDay < SunSet) {
AdjustLighting(false);
}
if (_timeOfDay > SunRise _timeOfDay < SunSet RenderSettings.skybox.GetFloat ("_Blend") < 1)
{
_tod = GameTime.TimeOfDay.SunRise;
BlendSkybox ();
} else if (_timeOfDay > SunSet RenderSettings.skybox.GetFloat ("_Blend") > 0) {
_tod = GameTime.TimeOfDay.SunSet;
BlendSkybox ();
}else{
_tod = GameTime.TimeOfDay.Idle;
}
}
private void BlendSkybox()
{
float temp = 0;
switch (_tod)
{
case TimeOfDay.SunRise:
temp = (_timeOfDay - SunRise) / _dayCycleInSeconds * skyboxBlendModifier;
break;
case TimeOfDay.SunSet:
temp = (_timeOfDay - SunSet) / _dayCycleInSeconds * skyboxBlendModifier;
temp = 1 - temp;
break;
}
RenderSettings.skybox.SetFloat("_Blend",temp);
}
private void SetUpLighting()
{
for (int cnt = 0; cnt <_sunScript.Length; cnt++)
{
if(_sunScript[cnt].giveLight)
{
sun[cnt].GetComponent<Light>().intensity = _sunScript[cnt]._minLightBrightness;
}
}
}
private void AdjustLighting(bool brighten)
{
if (brighten)
{
float pos = (_timeOfDay - SunRise) / _morningLength;
for(int cnt = 0;cnt <_sunScript.Length; cnt++)
{
if(_sunScript[cnt].giveLight)
{
Debug.Log(pos);
_sunScript[cnt].GetComponent<Light>().intensity = _sunScript[cnt]._maxLightBrightness * pos;
}
}
} else {
}
}
}
Any help would be greatly appreciated. Thanks!