How do I make this craftinfsystem Work

Joined
Feb 9, 2023
Messages
1
Reaction score
0
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();
}
}
 
Joined
Jan 30, 2023
Messages
107
Reaction score
13
It seems like the issue with your code is that you are trying to access the childCount of the lists slots, outsideSlots, and craftingSlots instead of the Transform objects stored within them. To access the childCount of the Transform objects stored within the lists, you should use slots.childCount, outsideSlots.childCount, and craftingSlots.childCount respectively.
Here's the updated checkSlots function hummm :

Code:
void checkSlots()
{
    //checks if slots are full
    for (int i = 0; i < slots.Count; i++)
    {
        if(slots[i].childCount > 0)
        {
            isFull[i] = true;
        }
        else
        {
            isFull[i] = false;
        }
    }

    for (int i = 0; i < outsideSlots.Count; i++)
    {
        if (outsideSlots[i].childCount > 0)
        {
            isFull[i + slots.Count] = true;
        }
        else
        {
            isFull[i + slots.Count] = false;
        }
    }

    for (int i = 0; i < craftingSlots.Count; i++)
    {
        if (craftingSlots[i].childCount > 0)
        {
            isFull[i + slots.Count + outsideSlots.Count] = true;
        }
        else
        {
            isFull[i + slots.Count + outsideSlots.Count] = false;
        }
    }
}


You should also make similar updates in the CraftItems function to access the childCount of the Transform objects stored within the slots list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top