# OnEnable()

> This function is called when the object is loaded.

## Definition

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

```csharp
public void OnEnable()
```

### Remarks

`OnEnable` is called whenever a ScriptableObject instance is loaded into memory. This happens in the following scenarios: \* At Editor startup, for all ScriptableObjects referenced in open scenes. \* On creation of a new ScriptableObject created as an asset via the [CreateAssetMenuAttribute](/engine/6000.7/script-reference/unityengine/createassetmenuattribute.md) in the Editor. \* On instantiation of a ScriptableObject instantiated at runtime via [ScriptableObject.CreateInstance](/engine/6000.7/script-reference/unityengine/scriptableobject/createinstance.md) or by runtime loading of the asset. \* On [domain reload](/engine/6000.7/manual/scripting/compilation-and-code-reload/code-reloading-editor/domain-reloading.md), for all ScriptableObjects loaded in memory on. \* On first loading a scene which contains a reference to the ScriptableObject in the [Hierarchy window](/engine/6000.7/manual/unity-editor/editor-windows-views-reference/hierarchy-window/hierarchy.md), or on subsequent loads if the original instance has since been garbage collected. \* On first selection of a ScriptableObject in the [Project window](/engine/6000.7/manual/unity-editor/editor-windows-views-reference/project-view.md), or on subsequent selections if the original instance has since been garbage collected. In most circumstances, `OnEnable` is only called once for a particular instance of a ScriptableObject asset in memory and is appropriate to use for initialization. If `OnEnable` is being called multiple times, it's likely that the ScriptableObject is being re-instantiated. This can happen for a variety of reasons. For example, if you deselect the ScriptableObject in the Project window or open a new scene that doesn't reference it, the object goes out of scope and can be garbage collected. Alternatively, events that trigger domain reloads, such as recompiling scripts or reimporting assets, can also cause ScriptableObjects to be re-instantiated and `OnEnable` to be called again. In the Editor, Unity also sometimes creates temporary instances of ScriptableObjects for property inspection or editing. Each temporary instance receives its own `OnEnable` call, but [ScriptableObject.OnDisable()](/engine/6000.7/script-reference/unityengine/scriptableobject/ondisable.md) might not be called if the object is garbage collected or destroyed without proper cleanup. `OnEnable` can't be a [coroutine](/engine/6000.7/manual/scripting/coroutines-section/coroutines.md).

### Examples

```csharp

// Right-click in the Project window > Create > Example > CounterData.
// Every time you recompile scripts or reload the asset, OnEnable is called, and the counter resets.

using UnityEngine;

[CreateAssetMenu(menuName = "Example/CounterData")]
public class CounterData : ScriptableObject
{
    public int counter;

    // Called when the ScriptableObject is loaded or reloaded
    void OnEnable()
    {
        Debug.Log("CounterData enabled! Resetting counter.");
        counter = 0; // Reset counter whenever enabled
    }
}
```
