DEV Community

Cover image for Digging into Unity: Scriptable Objects
Marc Mintel
Marc Mintel

Posted on

Digging into Unity: Scriptable Objects

Hey there, I'm Marc and I'm a professional web developer since a few years. It's time for something else so I decided to learn game development with C# and Unity. I'm going to use dev.to to keep my personal notes and summaries.

As I understand it so far a ScriptableObject is one of those things in Unity that you pick up pretty quickly but which is highly underestimated.

What's a ScriptableObject?

I find this name quite confusing, I've never heard of something like this in web development. I would say these are just objects that you can edit in Unity. But "just objects" is also a bit misleading, because they are not much different to MonoBehaviours. In fact they just don't have a transform and a GameObject, so they are not being updated each frame and therefor don't have a Start() or Update() method. You can add properties and methods to it but it won't execute any kind of update method on each frame.

Note: when you change the data of a ScriptableObject they are not reverted when you end the game. This has pros and cons of course.

How to create a ScriptableObject?

Create a new script and extend ScriptableObject instead of MonoBehaviour and remove the lifecycle methods Start and Update. The rest is up to you, e.g. you can add some public properties and methods.

using UnityEngine;

public class Foo : ScriptableObject {
    public float num = 1;
}
Enter fullscreen mode Exit fullscreen mode

Create a ScriptableObject from GUI

You can annotate your ScriptableObject class with a CreateAssetMenu.

using UnityEngine;

[CreateAssetMenu(fileName = "Foo", menuName = "Bar/Foo")]
public class Foo : ScriptableObject {
    public float num = 1;
}
Enter fullscreen mode Exit fullscreen mode

This will create a new entry in the context menu of Unity.

Context menu of ScriptableObjects

Note: when you create a new ScriptableObject the context menu it's saved as an .asset file.

Create a ScriptableObject from code

You can also create a ScriptableObject using its static method and store it in memory:

Foo mySO = ScriptableObject.CreateInstance<Foo>();
Enter fullscreen mode Exit fullscreen mode

What can you do with a ScriptableObject?

Generally a ScriptableObject is known to keep data often used for configs.
But actually you can do so much with it that I am sure just scratched the surface. You can do whole architecture systems based on ScriptableObjects as they are so powerful. You can use them for Events or to "replace" Enums. Especially because designers can work with them without touching a line of code, but I will cover this in another article (once I figured out).

Using a ScriptableObject in a MonoBehaviour

You can simply reference a ScriptableObject from a MonoBehaviour by using it's type definition, e.g to use the ScriptableObject named Foo and store it in a variable named mySO.

public class NewBehaviourScript : MonoBehaviour
{
    Foo mySO;
}
Enter fullscreen mode Exit fullscreen mode

That's it about ScriptableObjects. As I am still completely new on Unity take this with a grain of salt. The provided information are based on my own experiences and research. If you have something to add or correct please comment it.

Discussion (2)

Collapse
eelstork profile image
Tea

Just keep in mind that Scriptable objects depend on automated serialization, so this is brittle and not the best choice for long term persistence.

Collapse
mmintel profile image
Marc Mintel Author

Good to know, thank you!