NullReferenceException: Object reference not set to an instance of an object Interact.OnGUI () (at Assets/Interact.js:45)

In my game when the player looks at an object, a little gui message pops up. I created a chest model, and worked out a script that showed the gui. I now want to add torches however every time the player holds the mouse over the torch the console reads back: NullReferenceException: Object reference not set to an instance of an object
Interact.OnGUI () (at Assets/Interact.js:45) Any Ideas? Here is my script:

#pragma strict
var newSkin : GUISkin;

var open :int = 0;
var world : World;
function Start() {
    world = GameObject.Find("World").GetComponent("World");
}
function Update ()
{
    var hit : RaycastHit;
    if (Physics.Raycast(transform.position, transform.forward, hit, 5))
    {
    if (Input.GetKeyDown("x")){
        if(hit.collider.gameObject.tag == "chest")
        {
        if (open == 0){
            hit.collider.gameObject.animation.Play("Chest Opening");
            open = 1;
            world.SendMessage("AddChest");
           }

        }
        if(hit.collider.gameObject.tag == "torch")
        {
        // INSERT TORCH SCRIPT HERE
    }
    }
}
}


function OnGUI () {
var MyFont : Font;
var hit : RaycastHit;
 if (Physics.Raycast(transform.position, transform.forward, hit, 5))
    {if(hit.collider.gameObject.tag == "chest")
        { if (open == 0){
       GUI.skin = newSkin;
    GUI.Label (Rect (450, 600, 500, 40), "Press X to open!");
    }
    }
   }
   
    if(hit.collider.gameObject.tag == "torch")
        {
        GUI.Label (Rect (450, 600, 500, 40), "Press X to take!");
        }
}

One issue I see that your closing } for the raycast if is too soon - right after the if that checks whether the raycast hit a chest. Move the } so the torch code gets included in the block. Here is your version formatted properly. With proper indentation it’s a lot easier to notice these issues.

if (Physics.Raycast(transform.position, transform.forward, hit, 5)){
    if(hit.collider.gameObject.tag == "chest"){ 
        if (open == 0){
           GUI.skin = newSkin;
           GUI.Label (Rect (450, 600, 500, 40), "Press X to open!");
        }
    }
}

if(hit.collider.gameObject.tag == "torch"){
    GUI.Label (Rect (450, 600, 500, 40), "Press X to take!");
}  

Here’s the fixed version:

if (Physics.Raycast(transform.position, transform.forward, hit, 5)){
    if(hit.collider.gameObject.tag == "chest"){ 
        if (open == 0){
           GUI.skin = newSkin;
           GUI.Label (Rect (450, 600, 500, 40), "Press X to open!");
        }
    }

   if(hit.collider.gameObject.tag == "torch"){
        GUI.Label (Rect (450, 600, 500, 40), "Press X to take!");
   } 
}

make sure the names of your scripts/ prefabs are written the same way they are in this script

Please use proper indenting for your {…} blocks, it will help to prevent and detect errors such as this.

For example, your last statement if(hit.collider.gameObject.tag == "torch")is outside the if(Raycast), which means it will also be called if there is no hit, in which case hit.transform will be null, causing your error.

EDIT: darn, @phren was too fast ^^