Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed May 15 at 09:03 PM by Nakoru.
avatar image
0
Question by Nakoru · Jul 28, 2021 at 10:55 PM · uibuttonsinventoryinventory system

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 : alt text

All buttons have their functions defined :

alt text

image-2021-07-29-005414.png (28.6 kB)
image-2021-07-29-005521.png (55.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nakoru · Jul 29, 2021 at 05:34 AM 0
Share

The script is already attached to an empty gameObject, yet it doesn't work :/

avatar image jeffstaples1982 Nakoru · Jul 30, 2021 at 06:12 AM 0
Share

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

Answers Answers and Comments

218 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

scroll bar inventory with slot count for equip items 0 Answers

UI- Rotational Inventory,Where to begin? 2 Answers

How to flick back and forth in an inventory? 2 Answers

inventory system: drop item with mouseclick 1 Answer

Turn Another UI image on and off with a Single UI Button 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges