I started coding a crafting system, and it’s working, except for one small problem. It doesn’t check for every material, just one. I tried different ways of fixing it (foreach loop, for loop, if statement in both of those, etc.), and I’m stumped. Anybody know what I’m doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CraftButton : MonoBehaviour {
public CraftingRecipe recipe;
public Sprite icon;
public Button craftButton;
Inventory inventory;
public bool canCraft;
void Start ()
{
inventory = Inventory.instance;
}
void Update ()
{
CheckForMaterials();
if(canCraft == true)
{
craftButton.interactable = true;
} else
{
craftButton.interactable = false;
}
}
public void Craft()
{
if(canCraft == true)
{
inventory.Add(recipe.result);
RemoveMaterials();
}
else
{
Debug.Log("materials not found");
}
}
public void CheckForMaterials()
{
for (int i = 0; i < inventory.items.Count; i++)
{
if (inventory.items _== recipe.Materials*)*_
{
canCraft = true;
Debug.Log(“crafting materials found”);
break;
}
}
* }*
public void RemoveMaterials()
* {*
* for (int i = 0; i < recipe.Materials.Count; i++)*
* {*
_ inventory.Remove(recipe.Materials*);
}
}
}*_