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 Philip_Smyth · May 20, 2013 at 05:20 PM · guilistarraysinventory

Adding Item object to Inventory List

Hello all, I have been working on an inventory system for my dungeon crawler. To do this I have been converting JavaScript into C#(as it is what I am using) and I have ran into a bug that I do not know how to solve.

When I press the loot button, it will call the GiveLoot function, but this causes an error and the inventory remains empty. The first piece of code is the class the bug is apparantly in. I will put the error message below it followed by the other code to give a better understanding.

 using UnityEngine;
 using System.Collections;
 
 public class AddItem : MonoBehaviour {
     
     public Inventory Inventory;
     public ItemClass[] loot;
     
     void main()
     {}
     
     void GiveLoot()
     {
         for(int i = 0; i < loot.Length; i++)
         {
     
             this.Inventory.playerInventory.Add(this.loot[i]);
         }
     }
     
     void OnGUI()
     {
         GUILayout.BeginArea(new Rect((float)100, (float)0, (float) 50, (float)50));
         if(GUILayout.Button("Loot", new GUILayoutOption[0]))
         {
             this.GiveLoot();
         }
         GUILayout.EndArea();
     }
     
     // Use this for initialization
     void Start () 
     {
         this.Inventory = this.GetComponent("Inventory")as Inventory;
     }
     
     
 }


The error message(line 17):

NullReferenceException: Object reference not set to an instance of an object AddItem.GiveLoot () (at Assets/Inv Sys/script/AddItem.cs:17) AddItem.OnGUI () (at Assets/Inv Sys/script/AddItem.cs:26)

Now the Inventory:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Inventory : MonoBehaviour {
     
     public bool equipped;
     public ItemClass equippedItem;
     public Transform playerHand;
     public List<ItemClass> playerInventory = new List<ItemClass>();
     public Vector2 scrollView;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         if(this.equippedItem.itemType.Equals(ItemClass.ItemType.Weapon))
             this.equipped = true;
         else
             this.equipped = false;
     }
     
     
     void OnGUI()
     {
         GUILayout.BeginArea(new Rect ((float) (Screen.width - 500), (float)0, (float)500, (float)500));
         GUILayoutOption[]optionArray1 = new GUILayoutOption[] {GUILayout.Width((float)500), GUILayout.Height((float)500)};
         this.scrollView = GUILayout.BeginScrollView(this.scrollView, optionArray1);
         for(int i = 0; i < this.playerInventory.Count; i++)
         {
             GUILayout.BeginHorizontal(new GUILayoutOption[0]);
             
             if(GUILayout.Button(this.playerInventory[i].icon, new GUILayoutOption[0]) 
                 &&(this.playerInventory[i].itemType.Equals(ItemClass.ItemType.Weapon))
                 && !this.equipped)
             {
                 this.equippedItem = this.playerInventory[i];
                 Transform thing;
                 thing = (Transform)Object.Instantiate(this.playerInventory[i].itemPrefab, this.playerHand.position, Quaternion.identity);
                 thing.parent = this.playerHand;
                 this.playerInventory.RemoveAt(i);
                 return;
             }
             
             GUILayout.Box(this.playerInventory[i].name, new GUILayoutOption[0]);
             GUILayout.EndHorizontal();
             GUILayout.Box(this.playerInventory[i].description, new GUILayoutOption[0]);
         }
         
         GUILayout.EndScrollView();
         GUILayout.EndArea();
         
         if(GUI.Button(new Rect((float)((Screen.width / 2) - 0x19), (float)(Screen.height - 50), (float) 50, (float) 50), this.equippedItem.icon) && this.equipped)
         {
             this.playerInventory.Add(this.equippedItem);
             Object.Destroy(GameObject.FindGameObjectWithTag("Weapon"));
             this.equippedItem = new ItemClass();
         }    
             
     }
     
     
 }





Sorry for the length of the post, if you can help me at all I would really appreciate it. Any other questions please ask and I will let you know.

Thanks again and have a nice day :)

Comment
Add comment · Show 9
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 ExTheSea · May 20, 2013 at 05:24 PM 0
Share

Are you sure this. works? I know it does in Java but i don't know about this in Javascript/UnityScript or C# (never tried it because i would say it's not necessary). In line 17 in your first script you should just remove the this. part so that it's just:

 Inventory.playerInventory.Add(loot[i]);
avatar image Philip_Smyth · May 20, 2013 at 05:35 PM 0
Share

Yeah both options work the same way, either way i get the same error.

avatar image ExTheSea · May 20, 2013 at 05:40 PM 0
Share

Ah ok. I took a closer look and are you sure that the Inventory variable is filled at the time you're trying to access it?

avatar image Philip_Smyth · May 20, 2013 at 05:46 PM 0
Share

the inventory can be changed in the scene(as in how many slots are already taken) but there is no limit defined for the inventory. So i checked it with inventory being empty or filled and again there is no difference in the error

avatar image Tomer-Barkan · May 20, 2013 at 06:22 PM 0
Share

Try changing the Start() of AddItem as follows:

 void Start () 
 {
    this.Inventory = this.GetComponent<Inventory>();
 }

And please make sure in the inspector that every object that has "AddItem" script attached to it also has the "Inventory" script attached to it.

avatar image ExTheSea Tomer-Barkan · May 20, 2013 at 07:01 PM 0
Share

Are your sure this answer will work. I already mention the point that Inventory might be null. Also if the Inventory class is not attached to the same gameobject the addItem script is your code won't work.

avatar image Philip_Smyth Tomer-Barkan · May 20, 2013 at 07:07 PM 0
Share

Just tried it out and it works. Thank you guys so much for your help. Im trying to get the game done in a few weeks and you guys are life savers. Thanks again :)

avatar image Tomer-Barkan Tomer-Barkan · May 20, 2013 at 07:17 PM 0
Share

@ExTheSea: that the inventory is null was not a question, it's clear from the message... the only question was why. @Phillyp_Smyth: share with us what solved the issue. Was the second script not attached?

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

15 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

Related Questions

Problem with script 1 Answer

Hotbar and item pickups? 1 Answer

Scripting help (sending messages?) 1 Answer

Script Not Working 0 Answers

A node in a childnode? 1 Answer


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