Trouble setting up an inventory system to interact with game objects.

Hello, i’m trying to implement an invnetory system but having some issues. i made a script basedon some tutorials but am having some trouble when it comes to interact with a game object and making it add something to the inventory. Probably not using the best solutions because i’m not very good at programing. When i run the scripts there is no errors, but when i try to add the item it gives an error :

NullReferenceException: Object reference not set to an instance of an object
Tree01.tree01Window (Int32 windowID) (at Assets/Scripts/Tree01.js:36)
UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/GUI.cs:1395)

Here are the scripts i’m using:

Object script.

#pragma strict
var x = 0.0;
var y = 0.0;

private var wop : boolean = false;
private var gcc : boolean = false;
private var dgo : boolean = false;

function Start () {

}

function OnMouseOver(){
	if (Input.GetMouseButtonDown(1) && wop == false){
		wop = true;
	}
}

function OnGUI(){
	var mousePos = Vector3(Input.mousePosition.x, -(Input.mousePosition.y-Screen.height),0);
	var windowRect : Rect = Rect(x,y,150,80);
	if(Input.GetMouseButtonDown(1)){
		x = mousePos.x;
		y = mousePos.y;
	}
	if (wop==true){
		windowRect = GUI.Window (1, windowRect, tree01Window, "A tree, what now?");
	}
}

function tree01Window (windowID : int){
	
	if (GUI.Button(Rect(15,20,120,20), "Gather Wood")){
		print("Gathering wood...");
		var player = GameObject.Find("Player");
    	player.GetComponent(CollectResources).GetWood();
		
	}
	if (GUI.Button(Rect(15,50,120,20), "Let it be for now")){
		print("Leaving it...");
		gcc = true;
		closeWindow();		
	}
}

function closeWindow(){
	if (gcc == true){
		wop = false;
	}
}



function Update () {

}

Collect Resources script:

#pragma strict
import System.Collections.Generic;

var collectWood : ItemClass [];
var collectStone : ItemClass [];
var Inventory : Inventory;

function Start () {

	Inventory = GetComponent ("Inventory") as Inventory;

}

function OnGUI () {
		
}

function GetWood (){
	
	for (var x = 0; x < collectWood.Length; x++){
		Inventory.playerInventory.Add(collectWood[x]);
	}
}

function GetStone (){
	
	for (var x = 0; x < collectStone.Length; x++){
		Inventory.playerInventory.Add(collectStone[x]);
	}
}

Inventory:

#pragma strict

import System.Collections.Generic;

var playerInventory : List.<ItemClass> = new List.<ItemClass>();
var scrollView : Vector2;
var invSkin : GUISkin;
var invOpen : boolean = false;

function Update () {

	if (Input.GetKeyDown("i")){
		invOpen =! invOpen;
	}
}

function OnGUI () {

	var windowRect: Rect = Rect(Screen.width-400,0,400,200);
	if(invOpen == true){	
		windowRect = GUI.Window(0, windowRect, InvWindow, "Inventory");
		return;
	}
}

function InvWindow (windowID: int) {
	GUI.skin = invSkin;
	scrollView = GUILayout.BeginScrollView(scrollView, GUILayout.Width(380), GUILayout.Height(180));
//	if (GUI.Button(Rect(250,0,10,10), GUIContent (" ", "Close"))){
//		invOpen = false;
//	}
//	GUI.Label(Rect(250,20,100,40), GUI.tooltip); // displays tooltips on mouse over passed by GUIContent	
	for (var x = 0; x < playerInventory.Count; x++){
		GUILayout.BeginHorizontal();
		if(GUILayout.Button (playerInventory[x].icon, GUILayout.Width(25), GUILayout.Height(25)))
			{
			playerInventory.RemoveAt(x);
			return;
			}
		GUILayout.Box (playerInventory[x].name, GUILayout.Width(80), GUILayout.Height(25));
		GUILayout.Box (playerInventory[x].description, GUILayout.Width(190), GUILayout.Height(25)); 
		GUILayout.Box (playerInventory[x].weight, GUILayout.Width(20), GUILayout.Height(25));
		GUILayout.Box (playerInventory[x].ammount, GUILayout.Width(20), GUILayout.Height(25));		
		GUILayout.EndHorizontal();			
	}
	GUILayout.EndScrollView();
}

Any ideas on how to work around this? Thanks in advance.

The error is an null reference exception. It means that you are trying to access a member of an object that doesn’t exist (the variable or expression evaluates to null).

The error also states the callstack (the functions that were called to produce the error) and we can look at the top most one to figure out what causes the error. Let’s look at your error:

NullReferenceException: Object reference not set to an instance of an object 
Tree01.tree01Window (Int32 windowID) (at Assets/Scripts/Tree01.js:36)
...

It says the error happens in Tree01.js, at line 36. Looking at your code, we can check what goes on at line 36:

35:    var player = GameObject.Find("Player");
36:    player.GetComponent(CollectResources).GetWood();

There are two possible reasons line 36 could throw a NullReferenceException. Either you don’t have a game object called “Player” (that Line 35 is trying to find), or a “Player” game object does exist but there is no script on “Player” of type CollectResources.

I can’t tell which part of this is wrong, but it’s bound to be either of those.

It may be easier to understand if you broke it down into one more line:

35:    var player = GameObject.Find("Player");
36:    var resources = player.GetComponent(CollectResources);
37:    resources.GetWood();

Since player.GetComponent and resources.GetWood now are on two different lines, understanding the error will be easier. If the error happens on Line 36, you don’t have a GameObject in your scene called “Player”. If the error happens on Line 37, you don’t have a CollectResources script on the GameObject “Player”.

Thanks for the answer, it managed to solve the problem following your sugestion. Really good way of getting to know more accuratly where the error is, will definetly benefit from it in countless other ocasions. Turns out i had a “Player” in scene but the scripts were attatched to another object inside of the “Player”. All working now as intended.
Thanks again :slight_smile: