I have this code that I want to use in Unity to be able to check the childobjects within the craftingSlots. When I try to do this the code does not work and when I try to use the craftingSlots specific the IDs don't work. How should I make my code work in a way what when crafting the code only checks the childobjects of the craftingSlots?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryManager : MonoBehaviour
{
[Header("Inventory & Manager")]
public GameObject inventory;
//public GameObject outsideInventoryObject;
public Transform InventorySlotHolder;
public Transform InventoryHotbarSlotHolder;
public Transform OutsideInventory;
public Transform CraftingSlotHolder;
public Transform cursor;
public Vector3 offset;
public List<bool> isFull;
public List<Transform> slots;
public List<Transform> hotbarSlots;
public List<Transform> outsideSlots;
public List<Transform> craftingSlots;
public int currentSlot;
[Header("Rarity & Items")]
public Color[]rarityColors;
public Color defaultSlotColor;
public GameObject ItemToAdd;
public int AmountToAdd;
private void Start()
{
InitializeInventory();
//InitializeOutsideInventory();
SetSlotIDs();
SetSlotOutsideIDs();
SetCraftingSlotsIDs();
checkSlots();
AddItem(ItemToAdd);
DontDestroyOnLoad(this.gameObject);
}
private void Update()
{
if(inventory.activeSelf == true)
{
cursor.position = Input.mousePosition + offset;
}
if(cursor.childCount > 0)
{
cursor.gameObject.SetActive(true);
}
else
{
cursor.gameObject.SetActive(false);
}
}
void InitializeInventory()
{
//Sets Slots
for (int i = 0; i < InventorySlotHolder.childCount; i++)
{
slots.Add(InventorySlotHolder.GetChild(i));
isFull.Add(false);
}
for (int i = 0; i < InventoryHotbarSlotHolder.childCount; i++)
{
slots.Add(InventoryHotbarSlotHolder.GetChild(i));
hotbarSlots.Add(InventoryHotbarSlotHolder.GetChild(i));
isFull.Add(false);
}
for (int i = 0; i < OutsideInventory.childCount; i++)
{
slots.Add(OutsideInventory.GetChild(i));
hotbarSlots.Add(OutsideInventory.GetChild(i));
isFull.Add(false);
}
for (int i = 0; i < CraftingSlotHolder.childCount; i++)
{
slots.Add(CraftingSlotHolder.GetChild(i));
hotbarSlots.Add(CraftingSlotHolder.GetChild(i));
isFull.Add(false);
}
}
//void InitializeOutsideInventory()
//{
// for (int i = 0; i < OutsideInventory.childCount; i++)
// {
// outsideSlots.Add(OutsideInventory.GetChild(i));
// isFull.Add(false);
// }
//}
void checkSlots()
{
//checks if slots are full
for (int i = 0; i < slots.Count; i++)
{
if(slots.childCount > 0)
{
isFull = true;
}
else
{
isFull = false;
}
}
for (int i = 0; i < outsideSlots.Count; i++)
{
if (outsideSlots.childCount > 0)
{
isFull = true;
}
else
{
isFull = false;
}
}
for (int i = 0; i < craftingSlots.Count; i++)
{
if (craftingSlots.childCount > 0)
{
isFull = true;
}
else
{
isFull = false;
}
}
}
public void CraftItems(int[] IDs, int[] IDAmount, GameObject outcome, int outcomeAmount)
{
//collecting info if Item can be crafted or not
bool[] collected = new bool[IDs.Length];
for (int x = 0; x < IDs.Length; x++)
{
for (int i = 0; i < slots.Count; i++)
{
if (isFull == true && slots.childCount > 0)
{
Inventoryitem inventoryitem = slots.GetChild(0).GetComponent<Inventoryitem>();
if (inventoryitem.itemData.ID == IDs[x] && inventoryitem.amount >= IDAmount[x] )
{
collected[x] = true;
inventoryitem.amount -= IDAmount[x];
}
//if (inventoryitem.itemData.ID == IDs[x] && inventoryitem.amount >= IDAmount[x])
//{
// collected[x] = true;
// inventoryitem.amount -= IDAmount[x];
//}
}
}
}
for (int i = 0; i < collected.Length; i++)
{
if(collected == false)
{
return;
}
}
for (int i = 0; i < outcomeAmount; i++)
{
AddItem(outcome);
}
}
void SetSlotIDs()
{
for (int i = 0; i < slots.Count; i++)
{
if (slots.GetComponent<Slot>() != null)
{
slots.GetComponent<Slot>().ID = i;
}
}
}
void SetSlotOutsideIDs()
{
for (int i = 0; i < outsideSlots.Count; i++)
{
if (outsideSlots.GetComponent<Slot>() != null)
{
outsideSlots.GetComponent<Slot>().ID = i;
}
}
}
void SetCraftingSlotsIDs()
{
for (int i = 0; i < craftingSlots.Count; i++)
{
if (craftingSlots.GetComponent<Slot>() != null)
{
craftingSlots.GetComponent<Slot>().ID = i;
}
}
}
void AddItem(GameObject Item)
{
for (int i = 0; i < slots.Count; i++)
{
if (isFull == false)
{
//Add the Item
Instantiate(Item, slots);
isFull = true;
//checkSlots();
return;
}
else
{
Debug.Log("Slot is full");
}
}
Debug.Log("All slots are full");
}
//void AddItemOutside(GameObject Item)
//{
// for (int i = 0; i < slots.Count; i++)
// {
// if (isFull == false)
// {
// //Add the Item
// Instantiate(Item, outsideSlots);
// checkSlots();
// return;
// }
// else
// {
// Debug.Log("Slot is full");
// }
// }
// Debug.Log("All slots are full");
//}
public void PickUpDroppInventory()
{
if(slots[currentSlot].childCount > 0 && cursor.childCount < 1)
{
//Put inside cursor
Instantiate(slots[currentSlot].GetChild(0).gameObject, cursor);
Destroy(slots[currentSlot].GetChild(0).gameObject);
}
else if(slots[currentSlot].childCount < 1 && cursor.childCount > 0)
{
//Puts inside Slot
Instantiate(cursor.GetChild(0).gameObject, slots[currentSlot]);
Destroy(cursor.GetChild(0).gameObject);
}
else if(slots[currentSlot].childCount > 0 && cursor.childCount > 0)
{
if(slots[currentSlot].GetChild(0).GetComponent<Inventoryitem>().itemData.ID == cursor.GetChild(0).GetComponent<Inventoryitem>().itemData.ID)
{
if(slots[currentSlot].GetChild(0).GetComponent<Inventoryitem>().amount <= cursor.GetChild(0).GetComponent<Inventoryitem>().itemData.maxStack - slots[currentSlot].GetChild(0).GetComponent<Inventoryitem>().amount)
{
slots[currentSlot].GetChild(0).GetComponent<Inventoryitem>().amount += cursor.GetChild(0).GetComponent<Inventoryitem>().amount;
Destroy(cursor.GetChild(0).gameObject);
}
}
}
checkSlots();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryManager : MonoBehaviour
{
[Header("Inventory & Manager")]
public GameObject inventory;
//public GameObject outsideInventoryObject;
public Transform InventorySlotHolder;
public Transform InventoryHotbarSlotHolder;
public Transform OutsideInventory;
public Transform CraftingSlotHolder;
public Transform cursor;
public Vector3 offset;
public List<bool> isFull;
public List<Transform> slots;
public List<Transform> hotbarSlots;
public List<Transform> outsideSlots;
public List<Transform> craftingSlots;
public int currentSlot;
[Header("Rarity & Items")]
public Color[]rarityColors;
public Color defaultSlotColor;
public GameObject ItemToAdd;
public int AmountToAdd;
private void Start()
{
InitializeInventory();
//InitializeOutsideInventory();
SetSlotIDs();
SetSlotOutsideIDs();
SetCraftingSlotsIDs();
checkSlots();
AddItem(ItemToAdd);
DontDestroyOnLoad(this.gameObject);
}
private void Update()
{
if(inventory.activeSelf == true)
{
cursor.position = Input.mousePosition + offset;
}
if(cursor.childCount > 0)
{
cursor.gameObject.SetActive(true);
}
else
{
cursor.gameObject.SetActive(false);
}
}
void InitializeInventory()
{
//Sets Slots
for (int i = 0; i < InventorySlotHolder.childCount; i++)
{
slots.Add(InventorySlotHolder.GetChild(i));
isFull.Add(false);
}
for (int i = 0; i < InventoryHotbarSlotHolder.childCount; i++)
{
slots.Add(InventoryHotbarSlotHolder.GetChild(i));
hotbarSlots.Add(InventoryHotbarSlotHolder.GetChild(i));
isFull.Add(false);
}
for (int i = 0; i < OutsideInventory.childCount; i++)
{
slots.Add(OutsideInventory.GetChild(i));
hotbarSlots.Add(OutsideInventory.GetChild(i));
isFull.Add(false);
}
for (int i = 0; i < CraftingSlotHolder.childCount; i++)
{
slots.Add(CraftingSlotHolder.GetChild(i));
hotbarSlots.Add(CraftingSlotHolder.GetChild(i));
isFull.Add(false);
}
}
//void InitializeOutsideInventory()
//{
// for (int i = 0; i < OutsideInventory.childCount; i++)
// {
// outsideSlots.Add(OutsideInventory.GetChild(i));
// isFull.Add(false);
// }
//}
void checkSlots()
{
//checks if slots are full
for (int i = 0; i < slots.Count; i++)
{
if(slots.childCount > 0)
{
isFull = true;
}
else
{
isFull = false;
}
}
for (int i = 0; i < outsideSlots.Count; i++)
{
if (outsideSlots.childCount > 0)
{
isFull = true;
}
else
{
isFull = false;
}
}
for (int i = 0; i < craftingSlots.Count; i++)
{
if (craftingSlots.childCount > 0)
{
isFull = true;
}
else
{
isFull = false;
}
}
}
public void CraftItems(int[] IDs, int[] IDAmount, GameObject outcome, int outcomeAmount)
{
//collecting info if Item can be crafted or not
bool[] collected = new bool[IDs.Length];
for (int x = 0; x < IDs.Length; x++)
{
for (int i = 0; i < slots.Count; i++)
{
if (isFull == true && slots.childCount > 0)
{
Inventoryitem inventoryitem = slots.GetChild(0).GetComponent<Inventoryitem>();
if (inventoryitem.itemData.ID == IDs[x] && inventoryitem.amount >= IDAmount[x] )
{
collected[x] = true;
inventoryitem.amount -= IDAmount[x];
}
//if (inventoryitem.itemData.ID == IDs[x] && inventoryitem.amount >= IDAmount[x])
//{
// collected[x] = true;
// inventoryitem.amount -= IDAmount[x];
//}
}
}
}
for (int i = 0; i < collected.Length; i++)
{
if(collected == false)
{
return;
}
}
for (int i = 0; i < outcomeAmount; i++)
{
AddItem(outcome);
}
}
void SetSlotIDs()
{
for (int i = 0; i < slots.Count; i++)
{
if (slots.GetComponent<Slot>() != null)
{
slots.GetComponent<Slot>().ID = i;
}
}
}
void SetSlotOutsideIDs()
{
for (int i = 0; i < outsideSlots.Count; i++)
{
if (outsideSlots.GetComponent<Slot>() != null)
{
outsideSlots.GetComponent<Slot>().ID = i;
}
}
}
void SetCraftingSlotsIDs()
{
for (int i = 0; i < craftingSlots.Count; i++)
{
if (craftingSlots.GetComponent<Slot>() != null)
{
craftingSlots.GetComponent<Slot>().ID = i;
}
}
}
void AddItem(GameObject Item)
{
for (int i = 0; i < slots.Count; i++)
{
if (isFull == false)
{
//Add the Item
Instantiate(Item, slots);
isFull = true;
//checkSlots();
return;
}
else
{
Debug.Log("Slot is full");
}
}
Debug.Log("All slots are full");
}
//void AddItemOutside(GameObject Item)
//{
// for (int i = 0; i < slots.Count; i++)
// {
// if (isFull == false)
// {
// //Add the Item
// Instantiate(Item, outsideSlots);
// checkSlots();
// return;
// }
// else
// {
// Debug.Log("Slot is full");
// }
// }
// Debug.Log("All slots are full");
//}
public void PickUpDroppInventory()
{
if(slots[currentSlot].childCount > 0 && cursor.childCount < 1)
{
//Put inside cursor
Instantiate(slots[currentSlot].GetChild(0).gameObject, cursor);
Destroy(slots[currentSlot].GetChild(0).gameObject);
}
else if(slots[currentSlot].childCount < 1 && cursor.childCount > 0)
{
//Puts inside Slot
Instantiate(cursor.GetChild(0).gameObject, slots[currentSlot]);
Destroy(cursor.GetChild(0).gameObject);
}
else if(slots[currentSlot].childCount > 0 && cursor.childCount > 0)
{
if(slots[currentSlot].GetChild(0).GetComponent<Inventoryitem>().itemData.ID == cursor.GetChild(0).GetComponent<Inventoryitem>().itemData.ID)
{
if(slots[currentSlot].GetChild(0).GetComponent<Inventoryitem>().amount <= cursor.GetChild(0).GetComponent<Inventoryitem>().itemData.maxStack - slots[currentSlot].GetChild(0).GetComponent<Inventoryitem>().amount)
{
slots[currentSlot].GetChild(0).GetComponent<Inventoryitem>().amount += cursor.GetChild(0).GetComponent<Inventoryitem>().amount;
Destroy(cursor.GetChild(0).gameObject);
}
}
}
checkSlots();
}
}