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.
CloseFor 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.
CloseCalled when an instance of ScriptableObject
is created.
Awake
is called when a new instance of a ScriptableObject
is created. Scenarios in which this happens include:
ScriptableObject
is created as an asset via the Create Asset menu in the EditorScriptableObject
is created programmatically using ScriptableObject.CreateInstanceScriptableObject
is loaded from an AssetBundle at runtimeScriptableObject
asset is loaded in the Hierarchy window for the first time, or on subsequent loads if the original instance has since been cleaned up by Resources.UnloadUnusedAssetsScriptableObject
asset is selected in the Project window for the first time, or on subsequent selections if the original instance has since been cleaned up by Resources.UnloadUnusedAssets
Note: ScriptableObjects created as assets in Edit mode are not recreated on entering Play mode. To perform initialization work in a ScriptableObject
on entering Play mode, use ScriptableObject.OnEnable instead.
An example is given below. This example has two scripts. The first shown is the ScriptableObject script. This implements code which is separate from MonoBehaviour. The second is a small
MonoBehaviour related script which accesses values from the ScriptableObject script.
// 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 OnDestroy() { Debug.Log("OnDestroy"); } }
The following script makes use of the above 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)); } }