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.