Create items for apple juice, orange juice and water
using UnityEngine;
[System.Serializable]
public class Ingredient
{
public itemSO item;
public int amount;
}
[CreateAssetMenu(fileName = "RecipeSO", menuName = "Scriptable Objects/RecipeSO")]
public class RecipeSO : ScriptableObject
{
public Ingredient[] inputs;
public itemSO outputItem;
public int outputAmount;
}
panel_crafting - panel
panel menu - panel
apple juice - button
orange juice - button
panel craft recipe - panel
craftingRecipe slot - panel - with the grid layout
ingredientslot - prefab, image
ingredient icon - image
ingredientNumber - text
craftbutton - button
attach the Craft_blender to your blender model
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class Craft_blender : MonoBehaviour
{
public Inventory inventory;
public GameObject craftPanel;
public GameObject craftingRecipePanel;
public GameObject craftingRecipeSlot;
public RecipeSO[] juiceRecipe;
public GameObject ingredientslot;
RecipeSO recipeSelected = null;
public Button craftButton;
int craftavailableNumber = 0;
int selectedJuice=0;
bool craftStatus = false;
// Start is called once before the first execution of Update after the MonoBehaviour is created
private void Start()
{
//inventory = GetComponent<Inventory>();
craftPanel.SetActive(false);
craftingRecipePanel.SetActive(false);
craftButton.interactable = true;
}
public void Craft()
{
if(craftStatus)
{
inventory.AddItem(recipeSelected.outputItem, 1);
for (int i = 0; i < recipeSelected.inputs.Length; i++)
{
inventory.RemoveItem(recipeSelected.inputs[i].item, recipeSelected.inputs[i].amount);
}
inventory.refreshUI();
blenderjuice(selectedJuice);
//craftStatus = false;
}
}
public void blenderjuice(int juiceType)
{
craftStatus = false;
craftavailableNumber = 0; // IMPORTANT FIX
selectedJuice = juiceType;
foreach (Transform child in craftingRecipeSlot.transform)
Destroy(child.gameObject);
recipeSelected = juiceRecipe[juiceType];
craftingRecipePanel.SetActive(true);
if (inventory == null)
inventory = GameObject.Find("inventory").GetComponent<Inventory>();
for (int i = 0; i < recipeSelected.inputs.Length; i++)
{
GameObject slot = Instantiate(ingredientslot, craftingRecipeSlot.transform);
Transform iconTransform = slot.transform.Find("ingredientIcon");
Transform numberTransform = slot.transform.Find("ingredientNumber");
Image ingredientIcon = iconTransform.GetComponent<Image>();
TextMeshProUGUI numberText = numberTransform.GetComponent<TextMeshProUGUI>();
ingredientIcon.sprite = recipeSelected.inputs[i].item.icon;
int amountPlayerHas = inventory.GetItemAmount(recipeSelected.inputs[i].item);
numberText.text = amountPlayerHas + " / " + recipeSelected.inputs[i].amount;
if (inventory.HasItem(recipeSelected.inputs[i].item, recipeSelected.inputs[i].amount))
craftavailableNumber++;
}
if (craftavailableNumber >= recipeSelected.inputs.Length)
{
craftButton.interactable = true;
craftStatus = true;
}
else
{
craftButton.interactable = false;
}
}
public void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Player"))
{
craftPanel.SetActive(true);
}
}
public void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
craftPanel.SetActive(false);
craftingRecipePanel.SetActive(false);
}
}
}
using NUnit.Framework.Interfaces;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
public Dictionary<itemSO, int> items = new Dictionary<itemSO, int>();
public Transform inventoryPanel;
public GameObject inventorySlotPrefab;
public void AddItem(itemSO item, int amount)
{
if (!items.ContainsKey(item))
items[item] = 0;
items[item] += amount;
//Debug.Log($"Picked up {item.itemName}. Now have {items[item]}");
refreshUI();
//foreach(var no in items)
//{
// //Debug.Log(items);
//}
}
public bool HasItem(itemSO item, int amount)
{ return items.ContainsKey(item) && items[item]>=amount; }
public int GetItemAmount(itemSO item)
{
if (items.ContainsKey(item))
return items[item];
return 0;
}
public void RemoveItem(itemSO item, int amount)
{
if(items.ContainsKey(item))
{
items[item] -= amount;
if (items[item]<= 0)
items.Remove(item);
refreshUI();
}
}
public void refreshUI()
{
foreach (Transform child in inventoryPanel)
{
Destroy(child.gameObject);
}
foreach (var entry in items)
{
itemSO item = entry.Key;
int count = entry.Value;
GameObject slot = Instantiate(inventorySlotPrefab, inventoryPanel);
Transform iconTransform = slot.transform.Find("icon");
Transform countTransform = slot.transform.Find("count");
if (iconTransform == null || countTransform == null)
{
Debug.LogError("icon or count child not found in slot prefab!");
continue;
}
Image icon = iconTransform.GetComponent<Image>();
TextMeshProUGUI countText = countTransform.GetComponent<TextMeshProUGUI>();
if (icon != null)
{
icon.enabled = true;
icon.sprite = item.icon;
}
if (countText != null)
{
countText.enabled = true;
countText.text = count.ToString();
}
}
}
}
add 2 different collider to your blender model, the box collider is bigger than the model
apple button
orange button
craft button
uncheck the cursor lock