The cover for Unity: Create a Scriptable Object at Runtime

Unity: Create a Scriptable Object at Runtime

January 14, 2024

Managing your game’s data is paramount when building your game with Unity. Using Scriptable Objects is one of the best ways to organize your game’s data. In this article, you will learn how to create and use Scriptable Objects in your projects.

What is a Scriptable Object?

A ScriptableObject is a type of class in Unity that you can use to store large amounts of shared data independent of class instances. The use of Scriptable Objects makes debugging easier and stress-free. It helps in managing data in Unity, thereby reducing your Project’s memory. Each script has its unique value, limiting the number of copies of values. There is only one copy of a particular script in memory.

This is a series on data storage and user preferences in Unity. We recommend reading the series in order.

  1. Persistent Data Path in Unity
  2. Scriptable Objects in Unity
  3. Player Settings in Unity

How is a Scriptable Object different from other components in Unity?

When you create a MonoBehaviour script, it’s attached to a GameObject as a component in your project. However, you cannot attach a ScriptableObject to a GameObject, unlike MonoBehaviours.

ScriptableObjects are saved as Assets in your project. This allows you to create multiple versions of the same data, each with unique values. Also, you can save data to ScriptableObjects as an asset while using the Editor.

When we say Asset here, we don’t mean an asset like from the Unity Store. Instead, we are referring to an asset as a file in your project.

Now, how do you create a ScriptableObject at runtime?

How to Create a Scriptable Object at Runtime?

It is possible to create a ScriptableObject while the game or project is running. This can be achieved using the CreateInstance function.

newScriptableObject = ScriptableObject.CreateInstance<MyScriptableObjectType>()

The above code can be assigned to a variable to create a reference variable that will reference a new ScriptableObject asset. However, this asset is temporary as it’ll be terminated once the project stops running. Also, you won’t be able to edit the asset manually while the game runs.

You can also use the CreateAsset function of the Unity Editor’s Asset Database to save several asset instances to your project as files. However, this can only work in the Editor, not in the deployed environment.

UnityEditor.AssetDatabase.CreateAsset(scriptableObject, "path")

Ensure that the path you provide uses a native asset extension like “.mat”, “.cubemap”, “.asset”, “.anim”, and “.GUISkin”. Also, after you have used the CreateAsset function to create a new asset instance, you can add more assets to the file using the AssetDatabase.AddObjectToAsset.

Do you know that you can also create a Scriptable object in your project using the Create Asset Menu attribute?

[CreateAssetMenu]public class NewScript : ScriptableObject{}

With this, you have created a new ScriptableObject instance for your script. Now that you have learned how to create Scriptable Object instances for your project, you can put anything you like in it as they work like normal classes ranging from functions to references to real objects.

The fact that Scriptable Objects are assets makes them accessible even outside the scene, unlike regular script components. Therefore, using Scriptable Objects enhances your game’s data management.

Bottom Line

As a Unity game developer, if you have not been using Scriptable Objects before, you should start today and experience optimized organization of your game’s data.

Want to build better games?

© 2024