How is code after destroying gameObject running?

            Destroy(gameObject);

            Instantiate(explosionParticles, targetPosition, Quaternion.identity);
            onGrenadeExplode();

How are the lines after Destroy() running? I’ve always been under the impression that code stops executing when the object is destroyed, is this not true?

First, Hugo’s Instantiate happens before the Destroy().

Then, technically the object only gets destroyed at the end of the frame, so the code will still execute. The ‘link’ between the C# and the C++ code is severed (iirc) so you won’t be able to reference the game object anymore, but the function will complete if it is all still happening in the same frame.

1 Like

Think of Destroy() as queuing the GameObject to be destroyed at the end of the frame. This means any code that completes before the end of the frame will continue to do so.

1 Like

Privacy & Terms