right click script folder >> create >> scripting >> scriptableObject script, rename your script file as ItemData
//example code for ItemData.cs
using UnityEngine;
[CreateAssetMenu(fileName = "ItemData", menuName = "Inventory/ItemData")]
public class ItemData : ScriptableObject
{
public string itemName;
public Sprite icon;
public string description;
public bool isStackable;
}
right click >> create folder >> rename the folder as "Item"
right click >> create >>Inventory >> ItemData
explanation :
in our script ItemData.cs we have this line "[CreateAssetMenu(fileName = "ItemData", menuName = "Inventory/ItemData")]"
the menuName is Inventory/ItemData,
so when we create scriptable object, it will become Inventory >> ItemData
after create inventory >> itemData, you will see this
inspector there, you will see this, key in the correct value for your items.
google search an apple icon, then import to your unity, just drag and drop to unity
make sure your sprite mode change to single then remember click apply button
then for your icon, click the small circle to select the apple icon
then do these step for all the other items,
1. right click >> create inventory >> Itemdata
2.rename the items as "orange"
3. find an image for orange
4. change the sprite mode to single
5. key in the value for orange item
create 3 type of items using scriptable object (refer the above steps)
Create a folder for item prefab
drag and drop the apple sprite to your scene, then transform scale there, change to 0.1, 0.1, 0.1
add circle collider to your items, click edit collider and make sure the collider can wrap your item.
create script name "ItemPickup"
using UnityEngine;
public class ItemPickup : MonoBehaviour
{
public ItemData itemData;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
InventorySystem.instance.AddItem(itemData);
Destroy(gameObject);
}
}
}
attach this script to item prefab
create another monobehaviour script name it as InventorySystem
right click >> create >> scripting >> monobehaviour script
using System.Collections.Generic;
using UnityEngine;
public class InventorySystem : MonoBehaviour
{
public static InventorySystem instance;
// Store item and how many we have
public Dictionary<ItemData, int> itemDict = new Dictionary<ItemData, int>();
private void Awake()
{
if (instance == null) instance = this;
else Destroy(gameObject);
}
public void AddItem(ItemData item)
{
if (itemDict.ContainsKey(item))
{
itemDict[item]++;
}
else
{
itemDict[item] = 1;
}
Debug.Log("Picked up: " + item.itemName + " x" + itemDict[item]);
InventoryUI.instance.RefreshUI();
}
public Dictionary<ItemData, int> GetAllItems()
{
return itemDict;
}
}
create empty object and rename it as inventorySystem, attach your InventorySystem.cs to this gameobject
create ui invetory script
create >> scripting>> monobehaviour script >> rename the script as "InventoryUI "
attach this script to your canvas
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using TMPro;
public class InventoryUI : MonoBehaviour
{
public static InventoryUI instance;
public Transform inventoryPanel;
public GameObject inventorySlotPrefab;
void Awake()
{
instance = this;
}
void Start()
{
inventoryPanel.gameObject.SetActive(false);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.I))
{
inventoryPanel.gameObject.SetActive(!inventoryPanel.gameObject.activeSelf);
}
}
public void RefreshUI()
{
// Safety check for prefab
if (inventorySlotPrefab == null)
{
Debug.LogError("inventorySlotPrefab is not assigned!");
return;
}
// Clear old slots
foreach (Transform child in inventoryPanel)
{
Destroy(child.gameObject);
}
Dictionary<ItemData, int> items = InventorySystem.instance.GetAllItems();
foreach (var entry in items)
{
ItemData item = entry.Key;
int count = entry.Value;
GameObject slot = Instantiate(inventorySlotPrefab, inventoryPanel);
Transform iconTransform = slot.transform.Find("icon");
Transform countTransform = slot.transform.Find("count");
if (iconTransform == null || countTransform == null)
{
Debug.LogError("icon or count child not found in slot prefab!");
continue;
}
Image icon = iconTransform.GetComponent<Image>();
TextMeshProUGUI countText = countTransform.GetComponent<TextMeshProUGUI>();
if (icon != null)
{
icon.enabled = true;
icon.sprite = item.icon;
}
if (countText != null)
{
countText.enabled = true;
countText.text = count.ToString();
}
}
}
}
right click >> create >> UI >> Panel >> rename the panel as inventoryPanel
select your inventory panel then click add component button
add component >> select grid layout group
select your inventory panel then ui>> Image (slot to display your item)
select the panel and modify the padding value
select your inventoryPanel >> UI >> Text-textMeshPro
rearrange your game object as above
slot (Image)
icon(image)
count(text)
create a UI folder for your asset
drag and drop the slot as prefab
remember to fill in the slot for the script
using UnityEngine;
public class ItemPickup : MonoBehaviour
{
public string itemName;
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
Inventory.instance.AddItem(itemName);
Destroy(gameObject);
}
}
}
attach this script to your item such as apple, orange and grape
tick the is trigger checkbox
put your item like orange, grape and apple to scene
adjust your grid layout
add items to your scene, add collider, check the trigger, and add the ItemPickup script
example for grape