In the last blog, we talked about how you can store data using JSON/XML files. JSON and XML are excellent choices for saving structured data at runtime, like player progress or save files.
On the other hand, ScriptableObjects are great for storing static or design-time data.
Static data means information that has only one copy (shared by multiple scripts) — for example, base weapon stats, character attributes, or item definitions.
Design-time data means data you create beforehand while designing your game — something you write once and rarely update during gameplay.
One thing I like about ScriptableObjects is that they exist as assets in your project, so they retain their values even when you close and reopen Unity. They also help you separate data from logic, making your code cleaner and more modular.
Why Use ScriptableObjects?
- They’re more memory efficient than duplicating MonoBehaviour data.
- Multiple objects can share the same ScriptableObject instance.
- Perfect for storing configuration or reference data.
- They keep your project organized, especially when working with designers.
- Values are serialized and saved as
.assetfiles, so they persist across editor sessions.
How to Create a ScriptableObject
Unlike MonoBehaviours, you don’t attach ScriptableObjects to GameObjects.
Instead, you create a custom class that inherits from ScriptableObject, and then you can create instances of it as project assets.
To do that, use the CreateAssetMenu attribute — it lets you create ScriptableObject instances directly from the Unity Editor’s right-click menu.
Take a look at following example structure
using UnityEngine;
[CreateAssetMenu(fileName = "NewWeaponData", menuName = "Game/Weapon Data")]
public class WeaponData : ScriptableObject
{
public string weaponName;
public int damage;
public float range;
public float fireRate;
public Sprite icon;
}
Now, you can create an instance by right-clicking in your Project window:
Create → Game → Weapon Data
Unity will create a .asset file in your project folder that holds your weapon data.
You can then drag and drop this asset into any script field in the Inspector, just like any other reference.
The following is an example use case.
using UnityEngine;
public class Weapon : MonoBehaviour
{
public WeaponData weaponData;
private void Start()
{
if (weaponData != null)
{
Debug.Log($"Equipped: {weaponData.weaponName} | Damage: {weaponData.damage} | Range: {weaponData.range}");
}
else
{
Debug.LogWarning("No WeaponData assigned!");
}
}
public void Fire()
{
if (weaponData == null) return;
Debug.Log($"{weaponData.weaponName} fired! Damage: {weaponData.damage}");
// Add shooting logic here
}
}
Attach this script to Weapon GameObject, assign your created WeaponData asset in the Inspector, and you’re done!
Key Takeaway
ScriptableObjects are perfect for storing static, shared, or configuration data in Unity.
They keep your code modular, improve memory efficiency, and make it easy to manage data outside of scenes.
If you find yourself copying the same data into multiple prefabs or scripts, it’s time to consider moving that data into a ScriptableObject.
Top comments (0)