Hi. I made an inventory system made of a gameObject named Inventory with its script, a box that show current selected item and arrows to scroll through the List<> of items.
The problem here is that there’s no response when any button is clicked. Nothing. I cannot switch items or use them.
Inventory.cs :
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
using TMPro;
public class Inventory : MonoBehaviour
{
public List<Item> content = new List<Item>();
public int contentCurrentIndex = 0;
public Image itemImageUI;
public TextMeshProUGUI itemNameUI;
public Sprite emptyItemImage;
public PlayerEffects playerEffects; // attached to player
public static Inventory instance;
private void Awake()
{
if(instance != null)
{
Debug.LogWarning("Il y a plus d'une instance de Inventory dans la scène");
return;
}
instance = this;
}
private void Start()
{
UpdateInventoryUI();
}
public void ConsumeItem()
{
if(content.Count == 0)
{
return;
}
Item currentItem = content[contentCurrentIndex];
PlayerHealth.instance.HealPlayer(currentItem.hpGiven);
playerEffects.AddSpeed(currentItem.speedGiven, currentItem.speedDuration);
content.Remove(currentItem);
GetNextItem();
UpdateInventoryUI();
}
public void GetNextItem()
{
if (content.Count == 0)
{
return;
}
contentCurrentIndex++;
if(contentCurrentIndex > content.Count - 1)
{
contentCurrentIndex = 0;
}
UpdateInventoryUI();
}
public void GetPreviousItem()
{
if(content.Count == 0)
{
return;
}
contentCurrentIndex--;
if(contentCurrentIndex < 0)
{
contentCurrentIndex = content.Count - 1;
}
UpdateInventoryUI();
}
public void UpdateInventoryUI()
{
if(content.Count > 0)
{
itemImageUI.sprite = content[contentCurrentIndex].image;
itemNameUI.text = content[contentCurrentIndex].name;
}
else
{
itemImageUI.sprite = emptyItemImage;
itemNameUI.text = "";
}
}
}
Hierarchy :
All buttons have their functions defined :