Game Development
ItemData ScriptableObject
Learn to define item data with a ScriptableObject.
What is ScriptableObject?
ScriptableObject is a class type that stores game data as an asset. It does not need to be added to the scene; it sits as a project file and is used when needed. It is ideal for repeating data such as item information.
using UnityEngine;
[CreateAssetMenu(fileName = "YeniOge", menuName = "Envanter/Oge")]
public class ItemData : ScriptableObject
{
public string ogeAdi;
}
This class can now be created from the Project window.
ItemData and the asset
ItemData holds all the information about an item: its name, type and stack limit. Because this class is a ScriptableObject, a separate asset file is created for each item. This way the same data can be reused in many places.
oge.ogeAdi = "Kilic";
oge.yiginLimiti = 1;
Asset files are easily edited through the Inspector.
Exercise Create item data
In a class named OgeVerisi, define a string ogeAdi field with the value "Kilic" and an int yiginLimiti field with the value 5. In the Start method, print the pieces "Oge: ", ogeAdi, " Limit: " and yiginLimiti joined together with Debug.Log.
Show example solution
using UnityEngine;
public class OgeVerisi : MonoBehaviour
{
string ogeAdi = "Kilic";
int yiginLimiti = 5;
void Start()
{
Debug.Log("Oge: " + ogeAdi + " Limit: " + yiginLimiti);
}
}
Test yourself
-
What sets ScriptableObject apart?
- It must be added to the scene
- It speeds up lighting calculations
- It only builds the interface
- It stores data as an asset, without being added to the scene
Show answer
It stores data as an asset, without being added to the scene — A ScriptableObject is not a scene object but a project file, which is why it carries reusable data.
-
Derive the ItemData class
using UnityEngine;
public class ItemData : ____
{
public string ogeAdi;
}
Show answer
using UnityEngine;
public class ItemData : ScriptableObject
{
public string ogeAdi;
}
-
ScriptableObject terms
- ScriptableObject
- A class type that stores data
- Asset
- A project file
- ItemData
- The class that holds item info
- CreateAssetMenu
- The menu that creates a new asset
Next lesson: The Inventory class