Please help me - NullReferenceException

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 :confused:

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;
    }
}

Thanks for your response, it didn’t work though :confused:

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.

Thanks for your reply. This is what was logged in the console

the cam has no skybox component at all!
UnityEngine.Debug:Log(Object)
DayNight:Start() (at Assets/DayNightyCycle/DayNight.js:10)

NullReferenceException: Object reference not set to an instance of an object
DayNight.Start () (at Assets/DayNightyCycle/DayNight.js:12)

NullReferenceException: Object reference not set to an instance of an object
DayNight.Update () (at Assets/DayNightyCycle/DayNight.js:20)

What i get from this is, that there’s no skybox attached? If this is correct, then how do i create a skybox file?

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 :slight_smile: 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 :smile: Thanks for the help :slight_smile: