Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Xeladin · Jul 07, 2018 at 04:40 PM · 2duiinventoryclickinventory system

How to flick back and forth in an inventory?

Hi, I'm having an issue creating an inventory system, that it will be possible to flick back and forth through and increases in size to always hold the current amount of items, but never less than 10.

As far as I've seen people creating inventories they always display everything on the screen at the same time, which considering my inventory (in theory) can hold up to 60 items would make it a bit complicated. Instead I have been looking for a way to by defeault display item 0 through 9, and by clicking an arrow button, it would display item 1 - 10, 2 - 11, 3 - 12 and so forth until the items stop, at which the right arrow becomes inactive until another item is added.

alt text

This picture is a mockup of what I'm aiming for, by clicking the right arrow, all items would move 1 square left and vice versa, but as the purple ring is the last item displayed, the right arrow would be inactive. I have used the template provided by Unity's Adventure Game asset.

Code (CSharp):

 using UnityEngine;
 using UnityEngine.UI;
  
 public class Inventory : MonoBehaviour
 {
     public Image[] itemImages = new Image[numItemSlots];    // The Image components that display the Items.
     public Item[] items = new Item[numItemSlots];           // The Items that are carried by the player.
  
  
     public const int numItemSlots = 10;                      // The number of items that can be carried.  This is a constant so that the number of Images and Items are always the same.
  
  
     // This function is called by the PickedUpItemReaction in order to add an item to the inventory.
     public void AddItem(Item itemToAdd)
     {
         // Go through all the item slots...
         for (int i = 0; i < items.Length; i++)
         {
             // ... if the item slot is empty...
             if (items[i] == null)
             {
                 // ... set it to the picked up item and set the image component to display the item's sprite.
                 items[i] = itemToAdd;
                 itemImages[i].sprite = itemToAdd.sprite;
                 itemImages[i].enabled = true;
                 return;
             }
         }
     }
  
  
     // This function is called by the LostItemReaction in order to remove an item from the inventory.
     public void RemoveItem (Item itemToRemove)
     {
         // Go through all the item slots...
         for (int i = 0; i < items.Length; i++)
         {
             // ... if the item slot has the item to be removed...
             if (items[i] == itemToRemove)
             {
                 // ... set the item slot to null and set the image component to display nothing.
                 items[i] = null;
                 itemImages[i].sprite = null;
                 itemImages[i].enabled = false;
                 return;
             }
         }
     }
 }

I'm a bit reserved about asking for help to something that some would consider simple, but I just can't find anything that describes, exactly what I want to create. Any tips or help would be appreciated.

example.png (16.0 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MT369MT · Jul 07, 2018 at 06:57 PM

Hi, you could create a counter to know in wich position of the inventory you are. If you hit the right arrow you increase the counter +1, if you press the left arrow you do -1. Then you can do something like:

 for (int i = 0; i < 10; i++)
 {
 itemImages[i].sprite = items[i + counter].sprite;
 }


if you hit the right arrow 3 times the counter will be 3 and you will take the items from 0 + 3 to 9 + 3 that is 3 - 12.


Then you must create a condition to know if you are at the end of the inventory and you cant click the right/left arrow anymore for example:

 if (counter = 0)
     {
     //Cant press the left arrow anymore
     }
 if (counter = items.Lenght - 10)
     {
     //Cant press the right arrow anymore
     }
 
Comment
Add comment · Show 1 · 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 Xeladin · Jul 29, 2018 at 03:57 PM 0
Share

@$$anonymous$$T369$$anonymous$$T Where exactly would you put the counter? In the inventory or the buttons? And where exactly would you add them?

avatar image
0

Answer by Kanishk12 · Nov 09, 2019 at 06:29 AM

This has really made me think and I hope to read more. Would you like to know the details of ASTC Notification.

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

220 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 avatar image avatar image

Related Questions

Clicked position on UI image 0 Answers

Distinguish between clicks in 'game area' and on UI elements 1 Answer

chest items get added to inventory 2 Answers

Hover over text VERY glitchy 0 Answers

scroll bar inventory with slot count for equip items 0 Answers


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