The Inventory class
- Game Development course
- Advanced
- Lesson 27/71
- 4 min
Learn the Inventory class and slot logic.
The Inventory class
The Inventory class is the main component that holds the items the player carries. It contains a fixed number of slots; each slot holds one item type. The slot count decides the capacity.
[SerializeField] private int slotSayisi = 20;When the inventory is full, no new item can be added.
Slots and capacity
Each slot shows one item type and how many of that type are present. The capacity is the slot count itself; an inventory with 20 slots can carry at most 20 different item types. The same items stack in one slot.
int kapasite = 20;
int doluSlot = 5;Slot management is the basic operation of the inventory.
Practice Count the slots
In a class named SlotSayimi, define an int kapasite field with the value 20 and an int doluSlot field with the value 5. In the Start method, subtract doluSlot from kapasite and assign the result to an integer named bosSlot. Print the text "Bos slot: " joined with bosSlot using Debug.Log.
Starter code
using UnityEngine;
public class SlotSayimi : MonoBehaviour
{
// int kapasite alanini 20, int doluSlot alanini 5 degeriyle tanimla
void Start()
{
// kapasiteden doluSlot cikarip sonucu bosSlot degiskenine ata
// "Bos slot: " metnini bosSlot ile birlestirip yazdir
}
}Sample solution
using UnityEngine;
public class SlotSayimi : MonoBehaviour
{
int kapasite = 20;
int doluSlot = 5;
void Start()
{
int bosSlot = kapasite - doluSlot;
Debug.Log("Bos slot: " + bosSlot);
}
}Expected output
Bos slot: 15
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is a slot?
- The total capacity of an inventory
- A place reserved for a single item type Correct answer
- A button in the interface
- A list header where items are sorted
Explanation: A slot is the smallest unit of the inventory and is reserved for one item type.
2 Compute the empty slot count
int kapasite = 20;
int doluSlot = 5;
int bosSlot = kapasite - ____;
int kapasite = 20;
int doluSlot = 5;
int bosSlot = kapasite - doluSlot;
Explanation: The empty slot count is found by subtracting the filled slot count from the capacity, so the second term is doluSlot.
3 Inventory terms
| Term | Meaning |
|---|---|
Inventory |
The system that holds items |
Slot |
A place reserved for a single item type |
Kapasite |
The total slot count of the inventory |
Stack |
The pile of the same items in one slot |