OnDisable()
This function is called when the scriptable object goes out of scope.
Read time 1 minuteLast updated 6 days ago
Definition
- Type: Method
- Namespace: UnityEngine
public void OnDisable()
Remarks
OnDisableOnEnableOnDisableExamples
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!"); }}