The Basics Of Scriptable Objects In Unity

Scriptable Objects were a new concept to me as I started learning Unity. The basic idea is to create a standalone script independent of the scene, and which can’t be attached directly to any GameObject. Two main ways I’ve seen Scripted Objects used, are data persistence and templating.

Since Scripted Objects exist independent of any scene, you can use them to store data you’re going to need access to throughout your game. This might be things like the number of lives a player has, what weapons they have unlocked, or anything else you’re going to want to keep track of long term.

Scriptable Objects also make good templates, in case you have a large number of things to make that all share the same properties. This might be like cards in a card game or inventory items in an RPG. A scriptable object lets you define a set of properties once, and fill them in as many times as you need to with different data without the need to define a huge number of unique prefabs.

To get started with Scripted Objects, you need to inherit the ScriptableObject class.

No MonoBehavior here, just inherit ScriptableObject and add a CreateAssetMenu attribute, then add whatever data fields you want

From there just right-click to create an asset of your Scriptable Object using whatever menuName you specified.

Just fill in the blanks, and make as many assets as you want

That’s really all it takes to get started. Doubtlessly there are a lot of other clever ways to use Scriptable Objects, so knowing how they work is definitely a good idea if you're looking to get the most out of Unity.

Game developer with over 12 years of experience, mainly focused on MMOs.