Facebook

Unity: Create a Scriptable Object at Runtime

by Michael Sacco Published January 14, 2024
The cover for Unity: Create a Scriptable Object at Runtime

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.

Free download: Indie Game Marketing Checklist

Download now

Category

programming

Don't forget to share this post!

Popular assets for Unity

See all assets ->
    Cutting-edge volumetric fog and volumetric lighting with support for transparent materials.
    Volumetric clouds, day night cycles, dynamic skies, global lighting, weather effects, and planets and moons.
    A lightweight procedural skybox ideal for semi-stylized projects.
    Image-based Outlines for 2D and 3D games with variable line weight, color, and displacement options.
    Per-pixel gaussian blur on your entire screen, part of the UI, or in-scene objects.
    Drag-and-drop ready-to-use ambient, impact, and spell particle effects.

Free Indie Game Marketing Checklist

Learn how to make your game successful with this handy checklist.

Download for free