Here is my codes : Item Database: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using LitJson; using System.IO; using UnityEngine.UI; public class ItemDatabase : MonoBehaviour { private List<Item> database = new List<Item>(); private JsonData itemData; void Start() { itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Item.json")); ConstructItemDatabase(); Debug.Log(FetchItemByID(1).Description); } public Item FetchItemByID(int id) { for (int i = 0; i < database.Count; i++) { if (database[i].ID == id) return database[i]; } return null; } void ConstructItemDatabase() { for (int i = 0; i < itemData.Count; i++) { 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())); } } } public class Item { public int ID { get; set; } public string Title { get; set; } public int Value { get; set; } public int Power { get; set; } public int Defence { get; set; } public int Weight { get; set; } public string Description { get; set; } public bool Stackable { get; set; } public int Rarity { get; set; } public string Slug { get; set; } public string Type { get; set; } public Item(int id, string title, int value, int power, int defence, int weight, string description, bool stackable, int rarity, string slug, string type){ this.ID = id; this.Title = title; this.Value = value; this.Power = power; this.Defence = defence; this.Weight = weight; this.Description = description; this.Stackable = stackable; this.Rarity = rarity; this.Slug = slug; this.Type = type; } public Item() { this.ID = -1; } } Inventory Database: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class InventoryDatabase : MonoBehaviour { GameObject InvPanel; GameObject SlotPanel; ItemDatabase database; public GameObject InvSlots; public GameObject InvItems; public int slotAmount = 20; public List<Item> items = new List<Item>(); public List<GameObject> slots = new List<GameObject>(); // Use this for initialization void Start () { AddItem(0); database = GetComponent<ItemDatabase>(); InvPanel = GameObject.Find("InvPanel"); SlotPanel = InvPanel.transform.Find("SlotPanel").gameObject; for (int i = 0; i < slotAmount; i++) { items.Add(new Item()); slots.Add(Instantiate(InvSlots)); slots[i].transform.SetParent(SlotPanel.transform); } } public void AddItem (int id) { Item itemToAdd = database.FetchItemByID(id); for (int i = 0; i < items.Count; i++) { if (items[i].ID == -1) { items[i] = itemToAdd; GameObject itemObj = Instantiate(InvItems); itemObj.transform.SetParent(slots[i].transform); itemObj.transform.position = Vector2.zero; break; } } } } 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
database is null. You're doing this: Code (csharp): AddItem(0); 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.