using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
public List<SO_Items> inventoryConsumables_List;
public List<SO_Items> inventoryGatherables_List;
public GameObject gatherablesGrid;
//public GameObject inventoryButton;
public void InstantiateInventory()
{
//make everything inactive
MakeAllInactive();
//then activate only the amount we need
MakeActiveFromList();
}
public void MakeActiveFromList()
{
for (int a = 0; a < inventoryGatherables_List.Count; a++)
{
Transform inventoryBtn = gatherablesGrid.transform.GetChild(a);
inventoryBtn.gameObject.SetActive(true);
inventoryBtn.name = inventoryGatherables_List[a].item_name;
inventoryBtn.GetChild(0).GetComponent<Image>().sprite = inventoryGatherables_List[a].item_sprite;
inventoryBtn.GetChild(0).GetComponent<Image>().color = inventoryGatherables_List[a].item_color;
}
}
public void MakeAllInactive()
{
for (int a = 0; a < gatherablesGrid.transform.childCount; a++)
{
gatherablesGrid.transform.GetChild(a).gameObject.SetActive(false);
}
}
}
I tried researching but I could not find something that helps me. Maybe because english is not my native language and I research wrong…
I think that you should refactor your inventory data class - I assume that SO_Items is a class for inventory items - you could add an int field named for example amount, and then instead of adding more SO_Items just find if a particular item exists in inventory - if it does - increment amount value for that, if it doesn’t - add new with amount value set to 1
That sounds like a good idea, thank you.
SO_Items is just a scriptable object and i was just putting them in a list. But i will create a class and try it like you told me.
Edit: You solution was perfect. I was overcomplicated them in my head. I added a class as you told me with the number.
Inventory Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class InventoryItem : MonoBehaviour
{
public SO_Items so_Items;
public int number_of_SO_Items = 0;
public InventoryItem(SO_Items so_Items_constructor, int number_of_SO_Items_constructor)
{
so_Items = so_Items_constructor;
number_of_SO_Items = number_of_SO_Items_constructor;
}
}
public class Inventory : MonoBehaviour
{
public List<InventoryItem> inventoryConsumables_List;
public List<InventoryItem> inventoryGatherables_List;
public GameObject gatherablesGrid;
//public GameObject inventoryButton;
public void InstantiateInventory()
{
//make everything inactive
MakeAllInactive();
//then activate only the amount we need
MakeActiveFromList();
}
public void MakeActiveFromList()
{
for (int a = 0; a < inventoryGatherables_List.Count; a++)
{
Transform inventoryBtn = gatherablesGrid.transform.GetChild(a);
inventoryBtn.gameObject.SetActive(true);
inventoryBtn.name = inventoryGatherables_List[a].so_Items.item_name;
inventoryBtn.GetChild(0).GetComponent<Image>().sprite = inventoryGatherables_List[a].so_Items.item_sprite;
inventoryBtn.GetChild(0).GetComponent<Image>().color = inventoryGatherables_List[a].so_Items.item_color;
inventoryBtn.GetChild(1).GetComponent<TMP_Text>().text = inventoryGatherables_List[a].number_of_SO_Items.ToString();
}
}
public void MakeAllInactive()
{
for (int a = 0; a < gatherablesGrid.transform.childCount; a++)
{
gatherablesGrid.transform.GetChild(a).gameObject.SetActive(false);
}
}
}
And the code to compare if it exist and increase the number
Gathering Script
//loop time
for (int i = 0; i < inventory.inventoryGatherables_List.Count; i++)
{
if (inventory.inventoryGatherables_List[i].so_Items == so_Items)
{
alreadyInList = true;
indexFound = i;
break;
}
}
if (alreadyInList)
{
//check the max capacity
if (so_Items.item_max_inventory > inventory.inventoryGatherables_List[indexFound].number_of_SO_Items)
{
//just increment the number
inventory.inventoryGatherables_List[indexFound].number_of_SO_Items += 1;
}
else
{
//show notification
Notifications.ShowNotification("Max Capacity", "Cannot get " + so_Items.item_name + "! Reached max capacity!");
}
Debug.Log(inventory.inventoryGatherables_List[indexFound].so_Items.item_name + " :" + inventory.inventoryGatherables_List[indexFound].number_of_SO_Items);
}
else
{
InventoryItem inventoryItem = new InventoryItem(so_Items, 1);
//add the item to the list with 1 as number
inventory.inventoryGatherables_List.Add(inventoryItem);
}