Question by
FlamingFox1307 · Jun 21, 2020 at 09:41 AM ·
c#visual studioif-statementsfor-loop
If statement in for loop not working after fail
Hello! I've made a script to tell if the player can craft an item. If the player can't craft an item, but then tries again and can, they can't because hasEnough doesn't seem to be enabling correctly. Any help would be appreciated! Here's my code:
using TMPro;
using UnityEngine;
public class Button : MonoBehaviour
{
public Crafting recipe;
public TextMeshProUGUI descriptionText;
public void OnButtonClick()
{
var hasEnough = true;
descriptionText.text = recipe.description;
for (int i = 0; i < recipe.neededItems.Length; i++)
{
if (recipe.inventory.Container.Items[i].amount < recipe.neededItems[i].amount || recipe.inventory.Container.Items[i].ID != recipe.neededItems[i].id)
{
hasEnough = false;
}
}
if (hasEnough)
{
Debug.Log("crafted");
recipe.inventory.AddItem(new Item(recipe.item), 1);
}
else if (!hasEnough)
{
Debug.Log("not crafted");
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Can't Locate gameobject in array 3 Answers
Wait until a key is pressed inside a loop 1 Answer
Cross with cubes using for? 0 Answers
C# Greater Than or Equal To 1 Answer