Actions after Destroy(gameObject) is called?

22_br_cud
2d
#1

Hello!

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?

#2

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.

#3

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?

#4

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.

#5

Ah, that makes sense!

Thank you very much!

#6

You’re welcome. :slight_smile:


See also:

automatically bumped #7