Is OnDestroy() called on a MonoBehaviour immediately when Destroy(...) is called for its GameObject, or is it delayed until later in the frame when the destruction actually happens? I've got a few properties I've got to change when certain objects are destroyed. Since other stuff in the same frame will rely upon those changes I need to know whether OnDestroy() gets called immediately (and will thus do the job without any extra effort) or if I need to wrap it to make sure the changes are made when the destruction is flagged.
From what I've gathered, it's called in the same frame an object is marked for destruction, at the end of Update(). So basically "immediately".
If you need the object to be destroyed immediately, then use DestroyImmediate even though it's slower than Destroy. Destroy will destroy the object sometime during this frame, but you cannot expect the object to be destroyed during another object's Update.
I don't care when the object is destroyed. There's stuff I want to happen when the destruction is queued. If OnDestroy is called actually immediately then I can use that and it'll be transparent to other coders. If OnDestroy is not immediate (and the end of the same frame isn't immediate enough for me) then I'll have to wrap the Destroy method, and tell people not to use Destroy immediately for these particular objects. I'd rather the former approach, but it looks like I'll have to do an experiment first to ensure it works that way before relying on it.
So the answer is: OnDestroy called NOT immediatelly after Destroy(); Proofs: Code (CSharp): using UnityEngine; public class test_script : MonoBehaviour { void Start() { Debug.Log("Start1"); Destroy(gameObject); Debug.Log("Start2"); } private void Update() { Debug.Log("Update"); } private void OnDisable() { Debug.Log("OnDisable"); } private void OnDestroy() { Debug.Log("OnDestroy"); } } And the result: