# ScriptableObject

> A class you can derive from if you want to create objects that live independently of GameObjects.

## Definition

* **Type:** Class
* **Namespace:** [UnityEngine](/engine/6000.7/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule
* **Inherits from:** [Object](/engine/6000.7/script-reference/unityengine/object.md)

```csharp
public class ScriptableObject : Object
```

## Remarks

Use ScriptableObjects to centralise data in a way that can be conveniently accessed from scenes and assets within a project.

Instantiate ScriptableObject objects with [ScriptableObject.CreateInstance](/engine/6000.7/script-reference/unityengine/scriptableobject/createinstance.md).

You can save ScriptableObjects to asset files either from the Editor UI (see [CreateAssetMenuAttribute](/engine/6000.7/script-reference/unityengine/createassetmenuattribute.md)), or by calling [AssetDatabase.CreateAsset](/engine/6000.7/script-reference/unityeditor/assetdatabase/createasset.md) from a script. You can also generate ScriptableObjects as an output from a [ScriptedImporter](/engine/6000.7/script-reference/unityeditor/assetimporters/scriptedimporter.md). See [AssetImportContext.AddObjectToAsset](/engine/6000.7/script-reference/unityeditor/assetimporters/assetimportcontext/addobjecttoasset.md).

If a `ScriptableObject` has not been saved to an asset, and it's referenced from an object in a scene, Unity serializes it directly into the scene file. For ScriptableObjects that have only a single persistent instance within a project and are only used in Edit mode, you can use the [ScriptableSingleton\<T>](/engine/6000.7/script-reference/unityeditor/scriptablesingleton1.md) base class. For runtime singleton ScriptableObjects, you must implement your own singleton pattern and manage asset creation and loading manually.

Access previously saved objects using [AssetDatabase](/engine/6000.7/script-reference/unityeditor/assetdatabase.md), for example [AssetDatabase.LoadAssetAtPath](/engine/6000.7/script-reference/unityeditor/assetdatabase/loadassetatpath.md). When a ScriptableObject is referenced from a field on a [MonoBehaviour](/engine/6000.7/script-reference/unityengine/monobehaviour.md), the ScriptableObject is automatically loaded, so a script can simply use the value of the field to reach it.

The C# fields of a `ScriptableObject` are serialized exactly like fields on a MonoBehaviour, refer to [Script Serialization](/engine/6000.7/manual/scripting/compilation-and-code-reload/script-serialization.md) for details. Classes that include big arrays, or other potentially large data, should be declared with the [PreferBinarySerialization](/engine/6000.7/script-reference/unityengine/preferbinaryserialization.md) attribute, because YAML is not an efficient representation for that sort of data.

Calling `Destroy` on a `ScriptableObject` releases native resources associated with it but the object stays in memory until garbage collected. Objects in this detached state will appear to be null despite not really being so. However, this class doesn't support the [null-conditional operator](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#null-conditional-operator) (**?.**) and the [null-coalescing operator](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#the-null-coalescing-operator)(**??**).

The following example demonstrates a typical use of a ScriptableObject: different types of vehicle parameters are represented in the fields of a VehicleTypeInfo class, derived from ScriptableObject. Each type of vehicle would have its own asset file, with the parameter values set appropriately for the type.  Each instance of the vehicle in the game would have a reference to the asset corresponding to its type, rather than keeping its own redundant copy of each parameter.  This design makes it convenient to tweak vehicle behaviour in a central location. It is also good for performance, especially in cases where the size of the shared data is substantial.

The first script of the example implements a class derived from ScriptableObject.

```csharp
using UnityEngine;

[CreateAssetMenu]
public class VehicleTypeInfo : ScriptableObject
{
    // Class that represents a specific type of vehicle
    [Range(0.1f, 100f)]
    public float m_MaxSpeed = 0.1f;

    [Range(0.1f, 10f)]
    public float m_MaxAcceration = 0.1f;

    // This class could have many other vehicle parameters, such as Turning Radius, Range, Damage etc
}
```

The second script implements a MonoBehaviour that uses the ScriptableObject.

```csharp
using UnityEngine;
using UnityEditor;

public class VehicleInstance : MonoBehaviour
{
    // Snippet of a MonoBehaviour that would control motion of a specific vehicle.
    // In PlayMode it accelerates up to the maximum speed permitted by its type

    [Range(0f, 200f)]
    public float m_CurrentSpeed;

    [Range(0f, 50f)]
    public float m_Acceleration;

    // Reference to the ScriptableObject asset
    public VehicleTypeInfo m_VehicleType;

    public void Initialize(VehicleTypeInfo vehicleType)
    {
        m_VehicleType = vehicleType;
        m_CurrentSpeed = 0f;
        m_Acceleration = Random.Range(0.05f, m_VehicleType.m_MaxAcceration);
    }

    void Update()
    {
        m_CurrentSpeed += m_Acceleration * Time.deltaTime;

        // Use parameter from the ScriptableObject to control the behaviour of the Vehicle
        if (m_VehicleType && m_VehicleType.m_MaxSpeed < m_CurrentSpeed)
            m_CurrentSpeed = m_VehicleType.m_MaxSpeed;

        gameObject.transform.position += gameObject.transform.forward * Time.deltaTime * m_CurrentSpeed;
    }
}

public class ScriptableObjectVehicleExample
{
    [MenuItem("Example/Setup ScriptableObject Vehicle Example")]
    static void MenuCallback()
    {
        // This example programmatically performs steps that would typically be performed from the Editor's user interface
        // to creates a simple demonstration.  When going into Playmode the three objects will move according to the limits
        // set by their vehicle type.

        // Step 1 - Create or reload the assets that store each VehicleTypeInfo object.
        VehicleTypeInfo wagon = AssetDatabase.LoadAssetAtPath<VehicleTypeInfo>("Assets/VehicleTypeWagon.asset");
        if (wagon == null)
        {
            // Create and save ScriptableObject because it doesn't exist yet
            wagon = ScriptableObject.CreateInstance<VehicleTypeInfo>();
            wagon.m_MaxSpeed = 5f;
            wagon.m_MaxAcceration = 0.5f;
            AssetDatabase.CreateAsset(wagon, "Assets/VehicleTypeWagon.asset");
        }

        VehicleTypeInfo cruiser = AssetDatabase.LoadAssetAtPath<VehicleTypeInfo>("Assets/VehicleTypeCruiser.asset");
        if (cruiser == null)
        {
            cruiser = ScriptableObject.CreateInstance<VehicleTypeInfo>();
            cruiser.m_MaxSpeed = 75f;
            cruiser.m_MaxAcceration = 2f;
            AssetDatabase.CreateAsset(cruiser, "Assets/VehicleTypeCruiser.asset");
        }

        // Step 2 - Create some example vehicles in the current scene
        {
            var vehicle = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            vehicle.name = "Wagon1";
            var vehicleBehaviour = vehicle.AddComponent<VehicleInstance>();
            vehicleBehaviour.Initialize(wagon);
        }

        {
            var vehicle = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            vehicle.name = "Wagon2";
            var vehicleBehaviour = vehicle.AddComponent<VehicleInstance>();
            vehicleBehaviour.Initialize(wagon);
        }

        {
            var vehicle = GameObject.CreatePrimitive(PrimitiveType.Cube);
            vehicle.name = "Cruiser1";
            var vehicleBehaviour = vehicle.AddComponent<VehicleInstance>();
            vehicleBehaviour.Initialize(cruiser);
        }
    }
}
```

## Methods

| Method                                                                                   | Description                                                                                          |
| ---------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| [Awake](/engine/6000.7/script-reference/unityengine/scriptableobject/awake.md)           | Called when an instance of ScriptableObject is created.                                              |
| [OnDestroy](/engine/6000.7/script-reference/unityengine/scriptableobject/ondestroy.md)   | This function is called when the scriptable object will be destroyed.                                |
| [OnDisable](/engine/6000.7/script-reference/unityengine/scriptableobject/ondisable.md)   | This function is called when the scriptable object goes out of scope.                                |
| [OnEnable](/engine/6000.7/script-reference/unityengine/scriptableobject/onenable.md)     | This function is called when the object is loaded.                                                   |
| [OnValidate](/engine/6000.7/script-reference/unityengine/scriptableobject/onvalidate.md) | Editor-only function that Unity calls when the script is loaded or a value changes in the Inspector. |
| [Reset](/engine/6000.7/script-reference/unityengine/scriptableobject/reset.md)           | Reset to default values.                                                                             |

## Static Methods

| Method                                                                                           | Description                                 |
| ------------------------------------------------------------------------------------------------ | ------------------------------------------- |
| [CreateInstance](/engine/6000.7/script-reference/unityengine/scriptableobject/createinstance.md) | Creates an instance of a scriptable object. |

## Inheritance

**Inherited Members:**

* [Object.GetEntityId()](/engine/6000.7/script-reference/unityengine/object/getentityid.md)
* [Object.GetHashCode()](/engine/6000.7/script-reference/unityengine/object/gethashcode.md)
* [Object.InstantiateAsync\<T>(T)](/engine/6000.7/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, Transform)](/engine/6000.7/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, Vector3, Quaternion)](/engine/6000.7/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, Transform, Vector3, Quaternion)](/engine/6000.7/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int)](/engine/6000.7/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Transform)](/engine/6000.7/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Vector3, Quaternion)](/engine/6000.7/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, ReadOnlySpan\<Vector3>, ReadOnlySpan\<Quaternion>)](/engine/6000.7/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Transform, Vector3, Quaternion)](/engine/6000.7/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Transform, Vector3, Quaternion, CancellationToken)](/engine/6000.7/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Transform, ReadOnlySpan\<Vector3>, ReadOnlySpan\<Quaternion>)](/engine/6000.7/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Transform, ReadOnlySpan\<Vector3>, ReadOnlySpan\<Quaternion>, CancellationToken)](/engine/6000.7/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, InstantiateParameters, CancellationToken)](/engine/6000.7/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, InstantiateParameters, CancellationToken)](/engine/6000.7/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, Vector3, Quaternion, InstantiateParameters, CancellationToken)](/engine/6000.7/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, Vector3, Quaternion, InstantiateParameters, CancellationToken)](/engine/6000.7/script-reference/unityengine/object/instantiateasync.md)
* [Object.InstantiateAsync\<T>(T, int, ReadOnlySpan\<Vector3>, ReadOnlySpan\<Quaternion>, InstantiateParameters, CancellationToken)](/engine/6000.7/script-reference/unityengine/object/instantiateasync.md)
* [Object.Instantiate(Object, Vector3, Quaternion)](/engine/6000.7/script-reference/unityengine/object/instantiate.md#instantiate\(object-vector3-quaternion\))
* [Object.Instantiate(Object, Vector3, Quaternion, Transform)](/engine/6000.7/script-reference/unityengine/object/instantiate.md#instantiate\(object-vector3-quaternion-transform\))
* [Object.Instantiate(Object)](/engine/6000.7/script-reference/unityengine/object/instantiate.md#instantiate\(object\))
* [Object.Instantiate(Object, Scene)](/engine/6000.7/script-reference/unityengine/object/instantiate.md#instantiate\(object-scene\))
* [Object.Instantiate\<T>(T, InstantiateParameters)](/engine/6000.7/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate\<T>(T, Vector3, Quaternion, InstantiateParameters)](/engine/6000.7/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate(Object, Transform)](/engine/6000.7/script-reference/unityengine/object/instantiate.md#instantiate\(object-transform\))
* [Object.Instantiate(Object, Transform, bool)](/engine/6000.7/script-reference/unityengine/object/instantiate.md#instantiate\(object-transform-bool\))
* [Object.Instantiate\<T>(T)](/engine/6000.7/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate\<T>(T, Vector3, Quaternion)](/engine/6000.7/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate\<T>(T, Vector3, Quaternion, Transform)](/engine/6000.7/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate\<T>(T, Transform)](/engine/6000.7/script-reference/unityengine/object/instantiate.md)
* [Object.Instantiate\<T>(T, Transform, bool)](/engine/6000.7/script-reference/unityengine/object/instantiate.md)
* [Object.Destroy(Object, float)](/engine/6000.7/script-reference/unityengine/object/destroy.md)
* [Object.DestroyImmediate(Object, bool)](/engine/6000.7/script-reference/unityengine/object/destroyimmediate.md)
* [Object.FindObjectsByType(Type)](/engine/6000.7/script-reference/unityengine/object/findobjectsbytype.md#findobjectsbytype\(type\))
* [Object.FindObjectsByType(Type, FindObjectsInactive)](/engine/6000.7/script-reference/unityengine/object/findobjectsbytype.md#findobjectsbytype\(type-findobjectsinactive\))
* [Object.DontDestroyOnLoad(Object)](/engine/6000.7/script-reference/unityengine/object/dontdestroyonload.md)
* [Object.FindAnyObjectByType\<T>()](/engine/6000.7/script-reference/unityengine/object/findanyobjectbytype.md#findanyobjectbytypet\(\))
* [Object.FindAnyObjectByType\<T>(FindObjectsInactive)](/engine/6000.7/script-reference/unityengine/object/findanyobjectbytype.md#findanyobjectbytypet\(findobjectsinactive\))
* [Object.FindObjectsByType\<T>()](/engine/6000.7/script-reference/unityengine/object/findobjectsbytype.md#findobjectsbytypet\(\))
* [Object.FindObjectsByType\<T>(FindObjectsInactive)](/engine/6000.7/script-reference/unityengine/object/findobjectsbytype.md#findobjectsbytypet\(findobjectsinactive\))
* [Object.FindAnyObjectByType(Type)](/engine/6000.7/script-reference/unityengine/object/findanyobjectbytype.md#findanyobjectbytype\(type\))
* [Object.FindAnyObjectByType(Type, FindObjectsInactive)](/engine/6000.7/script-reference/unityengine/object/findanyobjectbytype.md#findanyobjectbytype\(type-findobjectsinactive\))
* [Object.ToString()](/engine/6000.7/script-reference/unityengine/object/tostring.md)
* [Object.name](/engine/6000.7/script-reference/unityengine/object/name.md)
* [Object.hideFlags](/engine/6000.7/script-reference/unityengine/object/hideflags.md)

### Operators

| Operator                                                                           | Description                                                             |
| ---------------------------------------------------------------------------------- | ----------------------------------------------------------------------- |
| [operator ==](/engine/6000.7/script-reference/unityengine/object/op-equality.md)   | Compares two object references to see if they refer to the same object. |
| [bool](/engine/6000.7/script-reference/unityengine/object/op-implicit.md)          | Determines whether the object exists.                                   |
| [operator !=](/engine/6000.7/script-reference/unityengine/object/op-inequality.md) | Compares if two objects refer to a different object.                    |
