注册碰撞回调函数,destroy销毁节点报错,延时无效仍报错

  • Creator 版本: 3.8.1

  • 目标平台:Chrome

  • 重现方式:
    start() {

      let collider = this.getComponent(BoxCollider2D);
    
      if(collider){
    
          collider.on(Contact2DType.PRE_SOLVE,this.onPreSolve,this);
    
      }
    

    }

    onPreSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null){

      if(otherCollider.tag == 2){
    
          this.scheduleOnce(()=>{
    
              otherCollider.node.destroy();
    
          },1);          
    
      }
    

    }

  • 首个报错:Cannot read properties of null (reading ‘destroy’)/nTypeError: Cannot read properties of null (reading ‘destroy’)

延迟一帧试试

写了延迟函数

感谢反馈,能整个复现demo吗?谢谢

写个判断 if (otherCollider && otherCollider.node) {
otherCollider.node.destroy();
} else {
console.warn(“otherCollider or otherCollider.node is null”);
}