using UnityEngine; using System.Collections.Generic; public class Inventory : MonoBehaviour { // A fixed array of 10 slots [Header("Exactly 10 Slots")] public Item[] slots = new Item[10]; public int currentSlot = 0; [SerializeField] private Transform handSlot; private GameObject currentSpawnedItem; void Update() { float scroll = Input.GetAxis("Mouse ScrollWheel"); if (scroll > 0f) { SelectPreviousSlot(); } else if (scroll < 0f) { SelectNextSlot(); } } /// /// Adds an item to the first available slot (if any). /// public void AddItem(Item newItem) { for (int i = 0; i < slots.Length; i++) { if (slots[i] == null) { slots[i] = newItem; break; } } // Optional: if no item is equipped yet, equip slot 0, etc. // if (currentSpawnedItem == null) EquipSlot(0); } private void SelectNextSlot() { currentSlot = (currentSlot + 1) % slots.Length; EquipSlot(currentSlot); // Force the UI to re-check which slot is active var ui = FindObjectOfType(); if (ui != null) { ui.RefreshUI(); } } private void SelectPreviousSlot() { currentSlot = (currentSlot - 1 + slots.Length) % slots.Length; EquipSlot(currentSlot); var ui = FindObjectOfType(); if (ui != null) { ui.RefreshUI(); } } public void EquipSlot(int slotIndex) { if (currentSpawnedItem != null) { Destroy(currentSpawnedItem); } Item itemInSlot = slots[slotIndex]; GameObject prefab = itemInSlot != null ? itemInSlot.GetPrefab() : null; if (prefab != null && handSlot != null) { currentSpawnedItem = Instantiate(prefab, handSlot.position, handSlot.rotation, handSlot); currentSpawnedItem.transform.localPosition = Vector3.zero; currentSpawnedItem.transform.localRotation = Quaternion.identity; } else { currentSpawnedItem = null; } } }