- Home /
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 :)
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]);
Yeah both options work the same way, either way i get the same error.
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?
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
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.
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.
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 :)
Your answer
Follow this Question
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