'Uncaught Error destroy() when create 2D Contact Physics in Cocos v3.4

Im using cocos dashboard 1.02 and cocos creator v3.4. And i have error when add this code: this.node.destroy(); to remove the prefab.

(i tried to folow DOC in coscos: https://docs.cocos.com/creator/manual/en/physics-2d/physics-2d-contact-callback.html?h=contact)

plz help me.

my code:

@ccclass('Ball')
export class Ball extends Component {
    start () {
        // Registering callback functions for a single collider
        let collider = this.getComponent(Collider2D);
        if (collider) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }
    }
    onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // will be called once when two colliders begin to contact
        this.node.destroy();//// ERROR WHEN I ADD THIS LINE
        console.log('onBeginContact');
    }
}

and my Game Manager

@ccclass('GameManager')
export class GameManager extends Component {

    @property({type: Prefab})
    public ballPrfb: Prefab|null = null;

    @property({type: Node})
    public Player: Node|null = null;

    @property({type: CCInteger})
    public _spawnTime: number = 2;

    private TimeFalling : number;

    start () {
       this.TimeFalling = 0;
    }
    update (deltaTime: number) {
        this.TimeFalling = this.TimeFalling - deltaTime;
        let block = instantiate(this.ballPrfb);
        
        if(this.TimeFalling <=0){
        this.node.removeAllChildren();
        this.node.addChild(block);
        block.setPosition(Math.random() * 800, 0, 0);
        this.TimeFalling = this._spawnTime;
        }
    }

In my console:

ErrorEventisTrusted: truebubbles: falsecancelBubble: falsecancelable: truecolno: 85composed: falsecurrentTarget: Window {window: Window, self: Window, document: document, name: '', location: Location, …}defaultPrevented: falseerror: Error
    at b2Body.SetActive (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/external/%2540cocos/box2d.js:1518:91)
    at b2RigidBody2D.setActive (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:176503:22)
    at b2RigidBody2D.onDisable (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:176353:16)
    at RigidBody2D.onDisable (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:257598:24)
    at ComponentScheduler.disableComp (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:150494:20)
    at NodeActivator._deactivateNodeRecursively (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:151568:48)
    at NodeActivator.activateNode (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:151420:18)
    at Node.set (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:149928:52)
    at Node.destroy (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:149621:25)
    at Ball.onBeginContact (http://localhost:7456/scripting/x/mods/fs/0/assets/script/Ball.js:47:21)eventPhase: 0filename: "http://localhost:7456/scripting/engine/bin/.cache/dev/preview/external/%2540cocos/box2d.js"lineno: 1518message: "Uncaught Error"path: [Window]returnValue: truesrcElement: Window {window: Window, self: Window, document: document, name: '', location: Location, …}target: Window {window: Window, self: Window, document: document, name: '', location: Location, …}timeStamp: 18327.200000047684type: "error"[[Prototype]]: ErrorEvent
(ẩn danh) @ index.js:1
box2d.umd.js:7181 Uncaught Error
    at b2Body.SetActive (box2d.umd.js:7181:15)
    at b2RigidBody2D.setActive (rigid-body.ts:202:21)
    at b2RigidBody2D.onDisable (rigid-body.ts:63:14)
    at RigidBody2D.onDisable (rigid-body-2d.ts:516:24)
    at ComponentScheduler.disableComp (component-scheduler.ts:439:22)
    at NodeActivator._deactivateNodeRecursively (node-activator.ts:326:50)
    at NodeActivator.activateNode (node-activator.ts:197:18)
    at Node.set (base-node.ts:172:54)
    at Node.destroy (base-node.ts:1199:13)
    at Ball.onBeginContact (Ball.ts:30:19)


Solution 1:[1]

setTimeout(() => {
    this.node.destroy();
}, 1);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vizivizler