- Home /
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.
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.
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
}
@$$anonymous$$T369$$anonymous$$T Where exactly would you put the counter? In the inventory or the buttons? And where exactly would you add them?
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.
Your answer
Follow this Question
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