Search Unity

Inventory System Problem

Discussion in 'Scripting' started by lefinno, Sep 14, 2017.

  1. lefinno

    lefinno

    Joined:
    Feb 20, 2014
    Posts:
    3
    Here is my codes :

    Item Database:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using LitJson;
    5. using System.IO;
    6. using UnityEngine.UI;
    7.  
    8. public class ItemDatabase : MonoBehaviour {
    9.     private List<Item> database = new List<Item>();
    10.     private JsonData itemData;
    11.  
    12.  
    13.     void Start()
    14.     {
    15.         itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Item.json"));
    16.         ConstructItemDatabase();
    17.         Debug.Log(FetchItemByID(1).Description);
    18.     }
    19.  
    20.     public Item FetchItemByID(int id) {
    21.         for (int i = 0; i < database.Count; i++)
    22.         {
    23.  
    24.             if (database[i].ID == id)
    25.  
    26.                 return database[i];
    27.         }
    28.             return null;
    29.      
    30.     }
    31.  
    32.  
    33.  
    34.     void ConstructItemDatabase() {
    35.         for (int i = 0; i < itemData.Count; i++) {
    36.             database.Add(new Item((int)itemData[i]["id"], itemData[i]["title"].ToString(), (int)itemData[i]["value"], (int)itemData[i]["stats"]["power"], (int)itemData[i]["stats"]["defence"], (int)itemData[i]["stats"]["weight"], itemData[i]["description"].ToString(), (bool)itemData[i]["stackable"], (int)itemData[i]["rarity"], itemData[i]["slug"].ToString(), itemData[i]["type"].ToString()));
    37.         }
    38.     }
    39. }
    40.  
    41. public class Item {
    42.     public int ID { get; set; }
    43.     public string Title { get; set; }
    44.     public int Value { get; set; }
    45.     public int Power { get; set; }
    46.     public int Defence { get; set; }
    47.     public int Weight { get; set; }
    48.     public string Description { get; set; }
    49.     public bool Stackable { get; set; }
    50.     public int Rarity { get; set; }
    51.     public string Slug { get; set; }
    52.     public string Type { get; set; }
    53.  
    54.     public Item(int id, string title, int value, int power, int defence, int weight, string description, bool stackable, int rarity, string slug, string type){
    55.         this.ID = id;
    56.         this.Title = title;
    57.         this.Value = value;
    58.         this.Power = power;
    59.         this.Defence = defence;
    60.         this.Weight = weight;
    61.         this.Description = description;
    62.         this.Stackable = stackable;
    63.         this.Rarity = rarity;
    64.         this.Slug = slug;
    65.         this.Type = type;
    66.  
    67.     }
    68.  
    69.    public Item() {
    70.         this.ID = -1;
    71.     }
    72. }
    Inventory Database:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class InventoryDatabase : MonoBehaviour {
    7.  
    8.     GameObject InvPanel;
    9.     GameObject SlotPanel;
    10.     ItemDatabase database;
    11.     public GameObject InvSlots;
    12.     public GameObject InvItems;
    13.  
    14.     public int slotAmount = 20;
    15.     public List<Item> items = new List<Item>();
    16.     public List<GameObject> slots = new List<GameObject>();
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.         AddItem(0);
    21.         database = GetComponent<ItemDatabase>();
    22.         InvPanel = GameObject.Find("InvPanel");
    23.         SlotPanel = InvPanel.transform.Find("SlotPanel").gameObject;
    24.         for (int i = 0; i < slotAmount; i++) {
    25.             items.Add(new Item());
    26.             slots.Add(Instantiate(InvSlots));
    27.             slots[i].transform.SetParent(SlotPanel.transform);
    28.         }
    29.     }
    30.  
    31.  
    32.  
    33.  
    34.     public void AddItem (int id) {
    35.        Item itemToAdd = database.FetchItemByID(id);
    36.        for (int i = 0; i < items.Count; i++) {
    37.             if (items[i].ID == -1) {
    38.                 items[i] = itemToAdd;
    39.                 GameObject itemObj = Instantiate(InvItems);
    40.                 itemObj.transform.SetParent(slots[i].transform);
    41.                 itemObj.transform.position = Vector2.zero;
    42.                 break;
    43.             }
    44.         }
    45.     }
    46. }
    47.  
    and it returns :
    NullReferenceException: Object reference not set to an instance of an object
    InventoryDatabase.AddItem (Int32 id) (at Assets/Scripts/InventoryDatabase.cs:35)
    InventoryDatabase.Start () (at Assets/Scripts/InventoryDatabase.cs:20)

    the error is clearly on this "Item itemToAdd = database.FetchItemByID(id);" line of code, yet i cannot seem to fix it, the function FetchByItemID() is alright, it works, yet i have no clue what happend
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    It might be the starts weren't in the right order. Maybe try putting the database in the awake.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    database is null. You're doing this:

    Code (csharp):
    1. AddItem(0);
    2. database = GetComponent<ItemDatabase>();
    so you're trying to add an item to the database before you've fetched it. That won't work. Just swap the lines.
     
    lefinno likes this.
  4. lefinno

    lefinno

    Joined:
    Feb 20, 2014
    Posts:
    3
    Thank you!!!!! I am so dumb!