# OnDisable()

> This function is called when the scriptable object goes out of scope.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.6/script-reference/unityengine.md)

```csharp
public void OnDisable()
```

### Remarks

`OnDisable` is called in the following scenarios: \* On loading a scene in the [Hierarchy window](/engine/6000.6/manual/unity-editor/editor-windows-views-reference/hierarchy-window/hierarchy.md), for any ScriptableObject that is currently loaded in memory but is not referenced in that scene. \* On invocation of [Resources.UnloadUnusedAssets](/engine/6000.6/script-reference/unityengine/resources/unloadunusedassets.md) for any ScriptableObject that was previously deselected in the [Project window](/engine/6000.6/manual/unity-editor/editor-windows-views-reference/project-view.md). \* On [domain reload](/engine/6000.6/manual/scripting/compilation-and-code-reload/code-reloading-editor/domain-reloading.md), for all ScriptableObjects loaded in memory. Subsequently `OnEnable` is called for these objects when they are recreated. `OnDisable` cannot be a [coroutine](/engine/6000.6/manual/scripting/coroutines-section/coroutines.md).

### Examples

```csharp

using UnityEngine;
using System;

public class EventManager
{
    public static Action OnSomethingHappened;

    public static void TriggerEvent()
    {
        OnSomethingHappened?.Invoke();
    }
}

// ScriptableObject that listens to an event and unsubscribes when disabled
[CreateAssetMenu(menuName = "Example/Event Listener SO")]
public class EventListenerSO : ScriptableObject
{
    void OnEnable()
    {
        Debug.Log("ScriptableObject enabled. Subscribing to event.");
        EventManager.OnSomethingHappened += HandleEvent;
    }

    void OnDisable()
    {
        Debug.Log("ScriptableObject disabled. Unsubscribing from event.");
        EventManager.OnSomethingHappened -= HandleEvent;
    }

    void HandleEvent()
    {
        Debug.Log("Event received by ScriptableObject!");
    }
}
```
