Scriptable Objects in Unity

Simon Pham
4 min readApr 29, 2024

--

What are scriptable objects in Unity?

In Unity, a scriptable object is a smart data container that can execute code and provides a designer-friendly experience. A common use for scriptable objects would be in an inventory system for RPG games.

Creating an inventory system

Using the conventional approach

First, let’s create an inventory system in Unity using the conventional class inheritance approach. I’m going to create a class called Item which will have all the base properties of an item for my game.

using UnityEngine;

[System.Serializable]
public class Item
{
public string itemName;
public int itemID;
public Sprite itemIcon;
public int gold;

}

Next, I’m going to create scripts for the equipables and consumables, which both will inherit from the Item class.

using UnityEngine;

[System.Serializable]
public class Equipable : Item
{
public int attack, strength, defense;
}
using UnityEngine;

[System.Serializable]
public class Consumable : Item
{
public int healthPoint;
}

After that, I’m going to create an empty object in the Hierarchy window called Inventory and a script for it. In my inventory system, I want to have all the item types that I created, and to do that I’d need to create three separate lists.

using System.Collections.Generic;
using UnityEngine;

public class Intventory : MonoBehaviour
{
public List<Item> itemDatabase;
public List<Consumable> consumableItemDatabase;
public List<Equipable> equipableItemDatabase;
}

And when I assign the script to the Inventory GameObject, I can manually add items to my lists.

By doing things this way, I as a developer will need to manage three lists at the same time and also this is not really a user-friendly approach for designers who will have to populate these lists.

Using the scriptable object approach

I’m going to create a separate folder called SO which stands for Scriptable Object to store all of my scripts for this approach and move all of the scripts that we created earlier into the Legacy folder.

Next, I’m going to create three scripts for the generic (SO_Item), equipable (SO_Equipable), and consumable (SO_Consumable) items. For the SO_Item script, I’m going to copy and paste all of the properties that we created for its Legacy counterpart, but we will have it inherit from ScriptableObject instead of nothing.

using UnityEngine;

public class SO_Item : ScriptableObject
{
public string itemName;
public int itemID;
public Sprite itemIcon;
public int gold;
}

For this approach, we will need to create the items in the Project window, so we’ll need to have that option in the menu and to do that we’re going to use the CreateAssetMenu attribute. You will need to specify the fileName and menuName properties. Since all scriptable objects are of asset type, we can use “item.asset” for fileName. The menuName property represents the display name shown in the Assets/Create menu, and I’m going to go with “Inventory/Regular Item”.

using UnityEngine;

[CreateAssetMenu(fileName = "item.asset", menuName = "Inventory/Regular Item")]
public class SO_Item : ScriptableObject
{
public string itemName;
public int itemID;
public Sprite itemIcon;
public int gold;
}

For the SO_Equipable and SO_Consumable scripts, they will inherit from the SO_Item class and we will also need to create menu options for them.

using UnityEngine;

[CreateAssetMenu(fileName = "item.asset", menuName = "Inventory/Equipable Item")]
public class SO_Equipable : SO_Item
{
public int attack, strength, defense;
}
using UnityEngine;

[CreateAssetMenu(fileName = "item.asset", menuName = "Inventory/Consumable Item")]
public class SO_Consumable : SO_Item
{
public int healthPoint;
}

In the SO folder, I’m going to create another folder to store all of our items called Items. To create a regular item, right-click the Items folder and select Create > Inventory > Regular Item, and I’m going to name it Scroll.

You can see a new asset file created under the Item folder.

And you can also populate all of its fields in the Inspector.

Now, let’s create an equipables and a consumable item which I’m going to name them Sword and Bread, respectively.

Next, let’s create an inventory list for these items. To do that, we can create an empty GameObject and a script for it which I’m going to call SO_Inventory.

using System.Collections.Generic;
using UnityEngine;

public class SO_Inventory : MonoBehaviour
{
public List<SO_Item> itemsDatabase;
}

Now in the Inspector, you can assign all of the items to this one single list which will be much easier to manage, and any designer can come in and create new items and populate the list easily.

--

--