- Home /
Question by
ramunas946 · Aug 13, 2021 at 04:25 PM ·
crafting
how to compere recipes and inventory for crafting?
public class CraftingItem : MonoBehaviour
{
public string Name;
public Sprite Image;
public List<InventorySlot> inventory;
public List<Recepie> recepie;
public List<int> toDeleteindex;
public bool isInInventory = false;
public bool CanCraft = false;
public int recepiecheck;
public void Start()
{
gameObject.transform.parent.transform.GetChild(1).GetComponent<Text>().text = Name;
gameObject.transform.parent.transform.GetChild(0).GetComponent<Image>().sprite = Image;
}
public void CraftItem()
{
inventory = GameObject.Find("Player").GetComponent<Inventory>().inventorySlot;
GameObject.Find("Player").GetComponent<Inventory>().UpdateInventory();
for (int i = 0; i < recepie.Count; i++)
{
for (int j = 0; j < inventory.Count; j++)
{
{
if (recepie[i].Name == inventory[j].Name)
{
if (recepie[i].Amount <= inventory[j].Amount)
{
toDeleteindex.Add(j);
recepiecheck +=1;
}
else
{
if (!toDeleteindex.Contains(j))
{
recepiecheck -=1;
return;
}
}
}
}
}
}
Debug.Log(recepiecheck);
if(recepiecheck == recepie.Count)
{
CanCraft = true;
}
else
{
CanCraft = false;
}
CraftItemitem();
CanCraft = false;
recepiecheck = 0;
GameObject.Find("Player").GetComponent<Inventory>().UpdateInventory();
}
public void CraftItemitem()
{
if (CanCraft == true)
{
if (isInInventory == true)
{
for (int i = 0; i < inventory.Count; i++)
{
if (inventory[i].Name == Name)
{
inventory[i].Amount += 1;
}
}
for (int i = 0; i < toDeleteindex.Count; i++)
{
inventory[toDeleteindex[i]].Amount -= recepie[i].Amount;
}
toDeleteindex.Clear();
}
else
{
for (int i = 0; i < toDeleteindex.Count; i++)
{
inventory[toDeleteindex[i]].Amount -= recepie[i].Amount;
}
inventory.Add(new InventorySlot(Name, 1, Image));
isInInventory = true;
toDeleteindex.Clear();
}
}
}
}
Comment
crafting works only if i have items in inventory that match recepie if i try to craft without items needed to craft it break all other crafting when item isn't crafted and i try to craft something else it just dos not work erros: ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Best Answer
Answer by ramunas946 · Aug 14, 2021 at 11:15 AM
i think i fixed it
public class CraftingItem : MonoBehaviour
{
public string Name;
public Sprite Image;
public List<InventorySlot> inventory;
public List<Recepie> recepie;
public List<int> toDeleteindex;
public bool isInInventory = false;
public bool CanCraft = false;
public int recepiecheck;
public void Start()
{
gameObject.transform.parent.transform.GetChild(1).GetComponent<Text>().text = Name;
gameObject.transform.parent.transform.GetChild(0).GetComponent<Image>().sprite = Image;
}
public void CraftItem()
{
inventory = GameObject.Find("Player").GetComponent<Inventory>().inventorySlot;
for (int i = 0; i < recepie.Count; i++)
{
for (int j = 0; j < inventory.Count; j++)
{
{
if (recepie[i].Name == inventory[j].Name)
{
if (recepie[i].Amount <= inventory[j].Amount)
{
toDeleteindex.Add(j);
recepiecheck += 1;
}
else
{
if (!toDeleteindex.Contains(j))
{
recepiecheck -= 1;
return;
}
}
}
}
}
}
for (int i = 0; i < toDeleteindex.Count; i++)
{
Debug.Log(toDeleteindex[i]);
}
if (recepiecheck == recepie.Count)
{
CanCraft = true;
}
else
{
CanCraft = false;
toDeleteindex.Clear();
}
CraftItemitem();
CanCraft = false;
recepiecheck = 0;
GameObject.Find("Player").GetComponent<Inventory>().UpdateInventory();
}
public void CraftItemitem()
{
var firstitem = true;
if (CanCraft == true)
{
for (int i = 0; i < inventory.Count; i++)
{
if (inventory[i].Name == Name)
{
inventory[i].Amount += 1;
firstitem = false;
break;
}
}
if (firstitem == true)
{
inventory.Add(new InventorySlot(Name, 1, Image));
}
for (int i = 0; i < toDeleteindex.Count; i++)
{
inventory[toDeleteindex[i]].Amount -= recepie[i].Amount;
}
toDeleteindex.Clear();
}
}
}
Your answer
Follow this Question
Related Questions
Crafting system 2 Answers
Problem at crafting 1 Answer
Simple Crafting and Inventory System 0 Answers
Crafting system NOT like minecraft 1 Answer
How can you make an inventory and crafting system? 0 Answers