- Home /
Inventory UI buttons not working
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 :
Answer by jeffstaples1982 · Jul 29, 2021 at 04:16 AM
@Nakoru The OnClick for the button needs to reference a GameObject with that script on it, if you use just a script nothing will happen. If you dont have the inventory script on an object you can just create an empty gameobject and throw it on it.
The script is already attached to an empty gameObject, yet it doesn't work :/
I built your entire script in a test environment and your script works perfectly fine, I was able to consume items and go through them. The issue has to be with the buttons, all I did was create the buttons and drag the Inventory GameObject to the buttons OnClick.
I did add your Item class to the Inventory script and made a few items in the inspector to test...can you see items on your screen?
I added this at the end of your Inventory script
[System.Serializable]
public class Item
{
public string name;
public int speedGiven;
public int speedDuration;
public Sprite image;
public int hpGiven;
}
BTW for the testing I did remove a few lines that was irrelevant but nothing that impacted the script, it was just item stuff
Follow this Question
Related Questions
How to flick back and forth in an inventory? 2 Answers
scroll bar inventory with slot count for equip items 0 Answers
UI- Rotational Inventory,Where to begin? 2 Answers
Images blocking buttons? 2 Answers
Is this considered bad coding 1 Answer