I’ve folowed a video tutorial on youtube on how to make a simple day / night cycle, but it seems that nomatter what i try i’ll always get these errors, can anyone help me fixing this please?
NullReferenceException: Object reference not set to an instance of an object
DayNight.Update () (at Assets/DayNightyCycle/DayNight.js:10)
NullReferenceException: Object reference not set to an instance of an object
DayNight.Update () (at Assets/DayNightyCycle/DayNight.js:14)
var theSun : GameObject;
var cam : GameObject;
var day : Material;
var night : Material;
function Update ()
{
if (theSun.light.enabled == true)
{
cam.GetComponent(Skybox).material = day;
}
else
{
cam.GetComponent(Skybox).material = night;
}
}
I have everything assigned in the inspector, which i would think was the problem
Yea i had a similar problem with get component. It kept giving me a null ref exception.
I would do something like this:
var theSun : GameObject;
var cam : GameObject;
var day : Material;
var night : Material;
var mySkybox : Skybox;
function Start()
{
if(cam != null)
{
mySkybox = cam.GetComponent<Skybox>(); //If you can use this implementation in javascript
}
}
function Update ()
{
if (theSun.light.enabled == true)
{
mySkybox.material = day;
}
else
{
mySkybox.material = night;
}
}
your cam object probably doesnt have the skybox component.
Try this
var theSun : GameObject;
var cam : GameObject;
var day : Material;
var night : Material;
function Start () {
if ( cam.GetComponent(Skybox) == null )
Debug.Log("the cam has no skybox component at all!");
if ( cam.GetComponent(Skybox).material == null )
Debug.Log("the skybox has no material at all!");
}
function Update ()
{
if (theSun.light.enabled == true)
{
cam.GetComponent(Skybox).material = day;
}
else
{
cam.GetComponent(Skybox).material = night;
}
}
And watch the console output, then report back what was logged.
that means your cam has no skybox component added. Check the tutorial you have been following again (try to take a look at the inspector of the cam object to see what the skybox component is. Maybe its just a normal skybox or a custom object made earlier in the tutorial) Camera’s dont have that by default so you must add it by yourself.
Yeah, going back and taking another look at the video did the trick I found out he added a component to the camera without showing or mentioning anything about it, so that’s what i did wrong… So obvious lol Thanks for the help