You are trying to destroy a object twice or more

Hi, I’m trying to destroy a node in Contact2DType.BEGIN_CONTACTcallback but get

You are trying to destroy a object twice or more. error.

I’m also checking for

if (this.node && this.node.isValid) this.node.destroy();

just to be sure, but nothing changes.

If I wait fo the next loop it works as expected

        setTimeout(() => {
            if (this.node && this.node.isValid) this.node.destroy();
        }, 0)

Any advice ?

This error can be debugged here to see where it is coming from.

I had similar issues over the years with for example destroy() and instantiate(), they are synchronous functions, but isn’t generated or destroyed completely synchronously, so you have to play around with setTimeout() sometimes to ensure you don’t crash your software. This happes mostly on bigger scenes/prefabs and/or slower devices.

A few years back I made an issue about this on Cocos github, my example wasn’t the best, since it is hard to replicate on a fresh small game and the ticket got closed .

thank you, it is not a big issue to move onto the next event loop but what is strange is that I cannot see others having this issue here in the forum. Anyway this thread contains a possible fix and an explanation, it’s fine to me.

Ive experienced the same issue when trying to destroy some nodes on onContactBegin. Let me try using a setTimeOut()