Actions after Destroy(gameObject) is called?


I just was wondering something. I noticed that you added a few actions to the Brick.cs in the DestroyBrick() method that occur AFTER the Destroy(gameObject).

How are those actions still working if the gameObject is being destroyed before-hand?

1 Like

Hi Cody,

That’s a good and important question. The Destroy method does not destroy the game object or instance immediately but with a delay granting us enough time to execute the rest of the code, too.

That’s good to know! Thank you! Has it always been that way? I’m coming back to Unity after 6-months or so as a refresher and I feel like that used to be a problem?

Also, do you know how long of a delay we have or will it just be sure to complete whatever cycle it’s doing before destroying?

Yes, it has always been this way.

I don’t know when exactly the object gets destroyed. The API says:
“Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering.”

Also see the script lifecycle flowchart.

Ah, that makes sense!

Thank you very much!

You’re welcome. :slight_smile:

See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.