Version: Unity 6.2 (6000.2)
LanguageEnglish
  • C#

ScriptableObject.OnEnable()

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual

Description

This function is called when the object is loaded.

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 Create Asset menu in the Editor.
  • On instantiation of a ScriptableObject instantiated at runtime via ScriptableObject.CreateInstance or by runtime loading of the asset.
  • On ___domain reload, for all ScriptableObjects loaded in memory on.
  • On first loading a scene which contains a reference to the ScriptableObject in the Hierarchy window, or on subsequent loads if the original instance has since been garbage collected.
  • On first selection of a ScriptableObject in the Project window, 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 might not be called if the object is garbage collected or destroyed without proper cleanup.

The following two part example shows a MonoBehaviour script that accesses values from a ScriptableObject.

// A ScriptableObject example script.
// The A and B members implement features which
// are unrelated to MonoBehaviour.

using UnityEngine;

public class ScriptObj : ScriptableObject { int a = 10; int[] b = new int[5] {0, 17, 34, 42, 67};

public int A { get {return a; } }

// return value in b array, or -1 if x is out-of-range public int B(int x) { if (x >= 0 && x < 5) return b[x]; else return -1; }

public void Awake() { Debug.Log("Awake"); }

public void OnEnable() { Debug.Log("OnEnable"); }

public void OnDisable() { Debug.Log("OnDisable"); }

public void OnDestroy() { Debug.Log("OnDestroy"); } }

The following script makes use of the previous ScriptableObject script:

// create and access the ScriptObj

using UnityEngine;

public class ScriptObjExample : MonoBehaviour { ScriptObj test;

void Start() { test = (ScriptObj)ScriptableObject.CreateInstance(typeof(ScriptObj));

print(test.A); print(test.B(3)); print(test.B(-3)); } }

OnEnable cannot be a co-routine.