public void AddItemsToList() { int count = 0; int id = 0; bool query = false; bool addOne = false; if (resetCompleted == true) { inventoryDB = new InventoryDatabase(); inventoryDB.ReadJsonData(); } foreach (var item in inventoryDB.items) { id = item.id; Item checkItemsInStorage = inventoryDB.FetchItemByID(id); if (checkItemsInStorage.storage > 1 && checkItemsInStorage.id != -3 && checkItemsInStorage.id != -2 && count < slotAmount) { for (int i = 0; i < checkItemsInStorage.storage; i++) { InstantiateAddedItems(checkItemsInStorage.id); } count++; query = true; } if (checkItemsInStorage.storage == 1 && checkItemsInStorage.id != -3 && checkItemsInStorage.id != -2 && count < slotAmount) { for (int i = 0; i < checkItemsInStorage.storage; i++) { InstantiateAddedItems(checkItemsInStorage.id); count++; } query = true; } if (count == slotAmount) { inventoryFull = true; } } resetCompleted = false; if (query == false) { Debug.Log("No items in storage!"); resetCompleted = false; } // here you can test instantiate if all item storages are 0 }
// Add item by id, check if they exists in database, instantiate them public void InstantiateAddedItems(int id) { if (resetCompleted == true) { inventoryDB = new InventoryDatabase(); inventoryDB.ReadJsonData(); } Item itemToAdd = inventoryDB.FetchItemByID(id); // Checks if item exists in database and stacks them by number if (itemToAdd.stackable == true && CheckIfItemExists(itemToAdd) && itemToAdd.id != -2) { for (int i = 0; i < items.Count; i++) { if (items[i].id == id && items[i].id != -2) { ItemData data = canvases[i].transform.GetChild(1).GetComponent <ItemData>(); if (data.amount != itemToAdd.storage) { data.amount++; data.transform.GetChild(0).GetComponent <TextMeshProUGUI>().text = data.amount.ToString(); } if (resetCompleted == true && itemToAdd.name == gameObjects[i].name && negativeValue == true) { GameObject go = gameObjects[i]; go.GetComponent <ItemData>().amount = data.amount; go.transform.GetChild(0).GetComponent <TextMeshProUGUI>().text = data.amount.ToString(); } break; } } } else { // loops all added items and instantiates objects for (int i = 0; i < items.Count; i++) { if (canvases[i].transform.Find("Placeholder") != null && canvases[i].transform.childCount == 1) { if (itemToAdd.id != -2) { if (resetCompleted == true) { i = 0 + (value); value++; } items[i] = itemToAdd; GameObject itemObj = Instantiate(Resources.Load($"Items/{itemToAdd.location}") as GameObject); itemObj.GetComponent <ItemData>().item = itemToAdd; itemObj.GetComponent <ItemData>().canv = i; itemObj.transform.SetParent(canvases[i].transform); itemObj.name = itemToAdd.name; itemObj.tag = "item"; itemObj.transform.position = Vector3.zero; itemObj.transform.localScale = Vector3.one; // testausta varten if if (itemObj.transform.position != GameObject.Find("Placeholder").transform.position) { itemObj.transform.position = canvases[i].transform.Find("Placeholder").position; } itemObj.GetComponent <Image>().raycastTarget = enabled; gameObjects.Add(itemObj); itemsCount++; itemsToFlush[i] = items[i]; } break; } } } }