How to get random stuff from chests?

How can I make a chest give a random item when its opened? I’ve searched for this and i could only find random spawn so help please. I also want to show the picture of the item that came out from the chest, like: “You got this” and picture.

Hi,

There are several ways to do this, an easy way to do is to create an array of items like:
//(in this case I have a base class called Item and item names extended to Item)

Item[] items = new Item[]{Potion, Helmet, Legs, Scoll01, Scroll02}; // possible items from chest

Item GetRandomItem(Item[] items){
         int randomIndex = Random.Rang(0, items.length);
         return items[randomIndex];
}

for an more advanced system I’d use struct classes to define what chance which item has to get out of a chest like:

[System.Serializable]
public struct ItemFromChestInfo{
         public Item item; // Prefab
         public short chance; // (0 to 99) %
}

[SerializeField]
private ItemFromChestInfo[] Items; // can be set in editor

Item GetRandomItemFromChest(ItemFromChestInfo[] items){
         float percentage = Random.Range(0,100); // random (0 to 99) %

         foreach (ItemFromChestInfo item in items) {
            // Check if random is in chance
            if (percentage < item.chance) {
                return item.item; // returns item
            }

            // Fix chance for next item
            percentage -= (int)item.chance;
         }
}

I hope this helped you out

Note: that the struct has to be defined after your class, ex:

   Class name{
    
    }
    
    [System.Serializable]
    public struct ....{
     ...
    }