Legacy Documentation: Version 2018.1 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

ScriptableObject.OnDisable()

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 scriptable object goes out of scope.

This is also called when the object is destroyed and can be used for any cleanup code. When scripts are reloaded after compilation has finished, OnDisable will be called, followed by an OnEnable after the script has been loaded.

#pragma strict
// A ScriptableObject example script.
// The A and B members implement features which
// are unrelated to MonoBehaviour.
public class ScriptObj extends ScriptableObject {
	var a: int = 10;
	var b: int[] = [0, 17, 34, 42, 67];
	intA {
		return a;
	}
	// return value in b array, or -1 if x is out-of-range
	public function B(x: int) {
		if (x >= 0 && x <= 5)return b[x];
		elsereturn -1;
	}
	public function Awake() {
		Debug.Log("Awake");
	}
	public function OnEnable() {
		Debug.Log("OnEnable");
	}
	public function OnDisable() {
		Debug.Log("OnDisable");
	}
	public function OnDestroy() {
		Debug.Log("OnDestroy");
	}
}
// 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 above ScriptableObject script.

#pragma strict
// create and access the ScriptObj
public class ScriptObjExample extends MonoBehaviour {
	var test: ScriptObj;
	function Start() {
		test = ScriptObjScriptableObject.CreateInstance(ScriptObj);
		print(test.A);
		print(test.B(3));
		print(test.B(-3));
	}
}
// 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)); } }

OnDisable cannot be a co-routine.

对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答