Create ScriptableObjects in Unity

ScriptableObject in Unity is a special type of class that allows the creation of reusable, data-driven objects. It's commonly used for storing and managing game data, configurations, and settings, but can also be used for a variety of purposes, such as managing game settings, creating data-driven systems, defining character abilities, storing dialogue or quest information, etc. It provides a flexible and efficient way to organize and manage data in Unity projects.

The main advantage of ScriptableObjects is that they provide a way to create data containers that can be easily shared and reused across multiple instances of scripts. They can be thought of as "blueprints" for creating instances of a specific data type. It's possible to create multiple instances of a ScriptableObject and modify their data independently.

Create a ScriptableObject

ScriptableObject can be created in the Unity editor and saved as assets in the project. Unlike MonoBehaviour scripts, ScriptableObject is not attached to GameObject in the scene but can be referenced and used by other scripts.

To create a ScriptableObject, follow the steps below:

  • In the Unity editor, right-click in the Project window and navigate to "Create -> C# ScriptableObject".
  • Give it a meaningful name and open the script in the preferred code editor.

Defining the ScriptableObject class:

  • In the script, inherit from the ScriptableObject class.
  • Define public fields or properties to represent the data to store in the ScriptableObject.
using UnityEngine;

[CreateAssetMenu(fileName = "NewData", menuName = "MyGame/ScriptableObject")]
public class MyScriptableObject : ScriptableObject
{
    public string dataName;
    public int dataValue;
}

In the example above, we define a ScriptableObject class with two public fields: 'dataName (string)' and 'dataValue (int)'.

Customizing the ScriptableObject in the Inspector:

  • Use attributes like '[SerializeField]' to expose specific fields to the Unity Inspector for modification while keeping those fields inaccessible from the script that will use that ScriptableObject.
using UnityEngine;

[CreateAssetMenu(fileName = "NewData", menuName = "MyGame/ScriptableObject")]
public class MyScriptableObject : ScriptableObject
{
    [SerializeField]
    private string dataName;

    [SerializeField]
    private int dataValue;
}

Creating instances of ScriptableObjects:

  • In the Unity editor, right-click in the Project window and select "Create" to create an instance of the ScriptableObject.
  • Customize the data of the created instance in the Inspector.

Accessing ScriptableObject data from other scripts:

  • In the MonoBehaviour script or any other script, create a public or serialized field of the ScriptableObject type and assign it in the Inspector view.
using UnityEngine;

public class MyBehaviour : MonoBehaviour
{
    public MyScriptableObject myData;
}
  • The data of the ScriptableObject instance then can be accessed through the assigned field.
using UnityEngine;

public class MyBehaviour : MonoBehaviour
{
    public MyScriptableObject myData;

    private void Start()
    {
        Debug.Log("Data Name: " + myData.dataName);
        Debug.Log("Data Value: " + myData.dataValue);
    }
}

Conclusion

ScriptableObjects are a versatile tool for managing and organizing data in game projects. By creating reusable and customizable data containers, ScriptableObjects offer a flexible and efficient way to store configurations, settings, and other non-instance-specific information. With the ability to create multiple instances of a ScriptableObject and modify their data independently, it becomes easy to create data-driven systems, manage game settings, define character abilities, store dialogue or quest information, and much more. Utilizing ScriptableObjects enhances the reusability, maintainability, and scalability of the Unity projects, ultimately streamlining the development process and empowering the creation of more robust and dynamic games.

Links
Unity 6