Getting error when trying to set otherCollider's RigidBody type in BeginContact callback

Hello Devs,

using cocos creator 3.1. I have two objects. One has box collider with sensor enabled and other object is a ball. In my scenario I drop this ball from top and what I want is when this ball touches the collider sensor, the ball should change its type from dynamic to static. But when I attempt to do that inside BeginContact callback of sensor, I get following error,


onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        if(otherCollider.tag == Globals.BALL_COLLIDER_TAG)
        {
            let ballBody = (otherCollider.node.getComponent(RigidBody2D) as RigidBody2D);
            ballBody.type = ERigidBody2DType.Static;
      }
  }
Uncaught Error
    at b2Body.SetType (box2d.umd.js:7090)
    at b2RigidBody2D.setType (rigid-body.ts:181)
    at RigidBody2D.set (rigid-body-2d.ts:69)
    at BasketNet.onBeginContact (BasketNet.ts:31)
    at BoxCollider2D.emit (callbacks-invoker.ts:342)
    at PhysicsContact.emit (physics-contact.ts:241)
    at PhysicsContactListener._onBeginContact [as _BeginContact] (physics-world.ts:364)
    at PhysicsContactListener.BeginContact (physics-contact-listener.ts:67)
    at b2PolygonAndCircleContact.Update (box2d.umd.js:11803)
    at b2ContactManager.Collide (box2d.umd.js:12479)

Am I doing something wrong? Thanks.