DEV Community

Cover image for Unity ScriptableObject
Giorgi Eliozashvili
Giorgi Eliozashvili

Posted on

Unity ScriptableObject

When you first make a weapon in a shooter game, you usually give it the standard MonoBehaviour script and write everything there: damage, fire rate, ammo, shooting logic, etc. But when you start adding new weapons, lots of data duplication appears. Every prefab holds a copy of the same data. To solve this, Unity has a feature called ScriptableObjects.

According to Unity documentation, this is the official use case of ScriptableObjects:

Saving and storing data during an Editor session
Saving data as an asset in your project to use at runtime


ScriptableObject is an independent container for saving data. Its main difference from the usual MonoBehaviour is that it does not need to be attached to a GameObject in the scene.

Let’s see the example of my basic weapon ScriptableObject:

using UnityEngine;

[CreateAssetMenu(fileName = "WeaponSO", menuName = "Scriptable Objects/WeaponsSO")]
public class WeaponSO : ScriptableObject
{
    public Weapon WeaponPrefab;
    public ParticleSystem HitVFX;

    public int Damage;
    public float FireRate;
    public bool IsAutomatic;
    public bool CanZoom;
    public float ZoomAmount;
    public float ZoomRotationSpeed;
}
Enter fullscreen mode Exit fullscreen mode

ScriptableObject-s are inheriting from ScriptableObject instead of MonoBehaviour.

Let’s break this example down:

  • [CreateAssetMenu] allows us to create a ScriptableObject file in our project's folder.

  • Inside the class, there are no Start() nor Update() methods. Just pure data that you can manipulate through Unity Inspector. And this is how it looks in Inspector:

ScriptableObject in inspector

So you can have separate weapon ScriptableObjects in your project and manipulate them more clearly and easily.


In summary, ScriptableObject is a perfect tool for data management, separating data (ScriptableObject) and logic (MonoBehaviour), making your project cleaner and easily manageable.

Top comments (0)