- Home /
Select all elements in a list?
Hi guys, Im trying to make my own inventory system. I had set up a code wich works with a raycast system and when the object with the desired tag is in front of the player, its picked up and stored in a list. The problem is that i cant figure out how to update the images from the Inventory UI acoording to that list. Actually I want to know if i can select all items inside the list and match the sprites with all the image Slot´s sprites. I would prefer no to code one by one, like in the example. Sorry about my english and the extensive context.
public List<Item> iList = new List<Item>();
public Image[] Slots;
void Update()
{
Slots[0].sprite = iList[0].icon;
}
Answer by BlakeSchreurs · Oct 04, 2017 at 01:56 PM
The following code will updated your icons every Update (once a frame). It'd generally be better to update only when the inventory changes (additions or removals), but that will depend heavily on the rest of your game code.
void Update () {
for (var i = 0; i < iList.Count; i++)
{
Slots[i].sprite = iList[i].icon;
}
}
Your answer
Follow this Question
Related Questions
unity2d inventory old style (like final fantasy) storing items amount issue C# 0 Answers
Issue with equipping object 0 Answers
How to get the current items in my inventory with respective stock of the items 1 Answer
Add to element quantity value if it already exists 0 Answers
Unknown Identifier 'List' 1 Answer