Item class
- Game Development course
- Intermediate
- Lesson 74/83
- 4 min
Learn to write a C# class named Item.
The Item Class
The Item class defines the common data structure for each object stored in the inventory. Every item can have fields such as a name, description, or value. Collecting this information in one class makes it easier to manage different item types within the same system.
public class Item
{
public string itemName;
public int value;
}Class Fields
Fields declared inside a C# class represent the data an object stores. For example, a string field named itemName can hold the item's name, while an int field named quantity can hold its quantity. Choosing the correct field types ensures that data is stored appropriately.
public string itemName;
public int quantity;Practice Define the Item Class
Create an independent C# class named Item. Inside the class, define the public string itemName field to store the item's name and the public int itemValue field to store its value.
Starter code
using UnityEngine;Sample solution
using UnityEngine;
public class Item
{
public string itemName;
public int itemValue;
}Expected output
Item sınıfı oluşturuldu
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which structure is used to define the shared data of each inventory item?
- The Camera component
- Animator Controller
- A Scene file
- The Item class Correct answer
Explanation: The Item class defines the shared fields and structure of item objects.
2 Item Name Field
Bir eşyanın adını metin olarak saklamak için C# dilinde ____ veri türü kullanılır.
Bir eşyanın adını metin olarak saklamak için C# dilinde string veri türü kullanılır.
Explanation: The string data type is used to store text information.
3 Match Item Fields
| Term | Meaning |
|---|---|
itemName |
The item's text name |
quantity |
The item's quantity |
value |
The item's numeric value |
description |
The item's description text |