onBeginContact: Error while set active property on node


Code example:
`@ccclass(“Typescript”)
export class Typescript extends Component {
start() {
this.getComponent(BoxCollider2D)!.on(
Contact2DType.BEGIN_CONTACT,
this.onBeginContact,
this
);
}

    onBeginContact(
        selfCollider: Collider2D,
        otherCollider: Collider2D,
        contact: IPhysics2DContact | null
    ) {
        otherCollider.node.active = false;
    }
}`

Project with example: test_v3.zip (4.9 KB)

Hi, you can not active a collider node in the contact callback, engine not allow this, do a delay will fix it.

I’m not sure what you mean. Could you explain the “delay” in more detail.

This fixed the error:

this.schedule(() => (otherCollider.node.active = false))

but it probably creates a lot of garbage on every call …

Yes, it is a way to delay, if you care garbage issue, you may keep other way, such as use node pool, process on update.