Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Marco Teles · Dec 21, 2013 at 11:04 AM · javascriptgameobjectinventory

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Statement · Dec 21, 2013 at 08:01 PM

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".

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Marco Teles · Dec 22, 2013 at 12:36 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to make a simple inventory: 0 Answers

Making changes to a terrian in game 1 Answer

Ai instantiates Game Object but won't stop. 0 Answers

Why does it give me this error and how can i fix it? 1 Answer

Track debug - Object dissapears after collision 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges