onBeginContact: Error while set active property on node

image
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.

  1. I have a pool of nodes, I did’t understand what you mean by “process on update”.
  2. is this a bug or is it explained in the documentation?

Not a bug, this is a code assertion made by box2d, and tell you what is the problem.

such as:

active_wait[] = []

update() {
for (var i in active_wait) {
if(active_wait[i].needActive)
active_wait[i].active = false
}
}

where should this code be located and how will the array be filled?
because filling the array will also create garbage.

Yes, an array needs to be created, this is just an idea, look at your own implementation.

why is it work in v2.4 and not work in v3 ?