Question by
Dimitrigaming · Jan 03, 2019 at 10:44 PM ·
listsinventory system
Add instantiated gameobject to a class list?
So i'm probably overcomplicating my inventory but I already made it and I'm trying to get it working. I have two scripts. My first one acts as my class list. My second one controls the inventory.
Class List
[Serializable]
public class InventorySlot
{
public int itemNumberInSlot;
public GameObject inventorySlot;
public bool slotUsed = false;
public int amountInSlot;
public Text quantityText;
public void AddSlot(GameObject newInventorySlot, Text newQuantityText)
{
inventorySlot = newInventorySlot;
quantityText = newQuantityText;
}
Inventory
public int amountOfSlots;
public GameObject invButton;
[HideInInspector]
public GameObject tempButton;
[HideInInspector]
public Text tempQuantity;
public GameObject buttonParent;
[HideInInspector]
public InventorySlot invSlot = new InventorySlot();
public List<InventorySlot> inventorySlots = new List<InventorySlot>();
private int slotNum;
void Start()
{
invButton.transform.SetParent(buttonParent.transform);
for (int i = 0; i < amountOfSlots; i++)
{
slotNum = i;
AddSlot();
}
public void AddSlot()
{
tempButton = Instantiate(invButton, buttonParent.transform);
tempQuantity = tempButton.transform.Find("Image/Quantity").GetComponent<UnityEngine.UI.Text>();
inventorySlots[slotNum].AddSlot(tempButton, tempQuantity);
}
Now the way I have it written, it does show up in the inspector, but it keeps returning a count of 0 in the list. That's where I'm stuck at right now, I'm trying to figure out how to add them to the list.
Comment
Answer by Dimitrigaming · Jan 04, 2019 at 09:45 PM
Alright, so it turns out that I do have the buttons adding to my list however, whenever I click one of the buttons I want it to tell me what slot it is, but they are all reading as slot 0. So that's my problem now.