Problem with error: Object reference not set to an instance of an object.

Sorry guys. I have a problem with one of my errors… I cannot fix it because I don’t know what shall I do… Could you help me? Please fix that error:

NullReferenceException: Object reference not set to an instance of an object
BushTake.Update () (at Assets/Inventory/BushTake.js:22)

As you can see I’m trying to get into my Inventory script.

#pragma strict

var respawnBarries : float = 100.0;
var EmptyBarries : boolean = false;
var Barries : GameObject;
var timerisactive : boolean = false;
var guiShow : boolean = false;
var timergui : float = 4.0;
var givebarries : boolean = false;


public var inventory : Inventory;

function Start () {

}

function Update () {
	
	if(givebarries == true)
	{
		inventory.barries += 1;
		givebarries = false;
	}
	
	if(timerisactive == true)
	{
		respawnBarries -= Time.deltaTime;
		EmptyBarries = false;
	}

	if(respawnBarries < 0)
	{
		Barries.SetActive (true);
		respawnBarries = 100.0;
	}
	
	if(timergui < 0)
	{
		guiShow = false;
	}

}

function OnTriggerEnter (Col : Collider)
{
  if(Col.tag == "BushPickup")
  {
	if(EmptyBarries == true)
	{

	}
	
	if(EmptyBarries == false)
	{
		Barries.SetActive (false);
		EmptyBarries = true;
		timerisactive = true;
		guiShow = true;
		givebarries = true;
	}
   }	
}

function OnTriggerExit(Col: Collider)
{
	if(Col.tag == "BushPickup")
	{
	
	}
}

function OnGUI()
{
	if(guiShow == true)
	{
	GUI.Box(Rect(Screen.width / 5 - 100, Screen.height / 2 - 160, 300, 20), "Barries has been automatically picked up");
	timergui -= Time.deltaTime;
	}
}

Your inventory variable is not assigned, i.e. empty. Hence, you get the NF error when you try to access a variable of it in Update().

The variable inventory either hasn’t been set in the editor window, or it’s not finding the object and setting during your scripts start-up (Awake/ Start).

If you’re not sure if the variable is going to be set, you can always check for null first.