Destoying from scene shows all children deleted, but still childrens are visible in scene on collision. on colllision

Destoying from scene shows all children deleted, but still childrens are visible in scene on collision : -

On collision with the saw, this is happening : -

onLoad() {
    this.animationComponent = this.node.getComponent(Animation);
    this.animationComponent.clips[0].name = AnimationNames.IDLE;
    this.animationComponent.clips[1].name = AnimationNames.RUN;
    this.animationComponent.clips[2].name = AnimationNames.DANCE_1;
    this.animationComponent.clips[3].name = AnimationNames.DANCE_2;

    this.characterBody.getComponent(RigidBody).useCCD = true;
    this.characterBody
      .getComponent(BoxCollider)
      .on("onCollisionEnter", this.onCollisionEnter, this);
  }

  onCollisionEnter(event: ICollisionEvent) {
    const otherColliderName = event.otherCollider.name;
    const selfColliderName = event.selfCollider.name;

    if (otherColliderName.includes("Ground-Mesh")) {
      // Handle Ground-Mesh collision
    } else if (otherColliderName.includes("ramp")) {
      // Handle ramp collision
    } else if (otherColliderName.includes("saw")) {
      console.log("saw", this.node.getParent().children);

      this.node
        .getParent()
        .getComponent(CharactersManager)
        .removeCharacter(this.node); // Ensure you pass the correct node
    } else if (otherColliderName.includes("knife")) {
      // Handle knife collision
    } else if (otherColliderName.includes("hammer")) {
      // Handle hammer collision
    } else if (otherColliderName.includes("LeftSide")) {
      // Handle LeftSide collision
    } else if (otherColliderName.includes("RightSide")) {
      // Handle RightSide collision
    }
  }

From this, call is going to the parent i.e. -

 removeCharacter(character: Node) {
    const characterUUID = character.uuid;
    let characterToBeRemoved = null;

    // Find the character to be removed
    for (let i = 0; i < this.characters.length; i++) {
      if (this.characters[i].uuid === characterUUID) {
        characterToBeRemoved = { node: this.characters[i], index: i };
        break; // Exit the loop once the character is found
      }
    }

    // Remove the character if found
    if (characterToBeRemoved) {
      const nodeToRemove = characterToBeRemoved.node;

      // Stop all animations on the node
      const animationComponent = nodeToRemove.getComponent(Animation);
      if (animationComponent) {
        animationComponent.stop();
      }

      // Destroy the character node
      nodeToRemove.destroy();

      // Remove from characters array
      this.characters.splice(characterToBeRemoved.index, 1);

      // Update children count
      this.charactersCurrentlyInScene--;

      // Remove from parent node's children array
      if (nodeToRemove.parent) {
        const indexInChildren =
          nodeToRemove.parent.children.indexOf(nodeToRemove);
        if (indexInChildren !== -1) {
          nodeToRemove.parent.children.splice(indexInChildren, 1);
        }
      }
    }
  }

At the end, complete array is consoled as empty array, but childrens are still visible in the scene. What’s happening wrong with the collision detection code? How can i fix this ?

You can add breakpoints in the two functions, node.destroy and node.setParent , and then observe through debugging whether these functions are being executed, and also check if the node that is being operated on is the one you expect.

Thanks for replying, Actually i got the error, Its because of the the below code only. Thing happening was it was removing 2 nodes at a time, but only destoying 1 node at a time. Forgot that destoying a node removes it from its parent also.

// Destroy the character node
      nodeToRemove.destroy();

      // Remove from characters array
      // this.characters.splice(characterToBeRemoved.index, 1);

      // Update children count
      // this.charactersCurrentlyInScene--;

      // Remove from parent node's children array
      // if (nodeToRemove.parent) {
      //   const indexInChildren =
      //     nodeToRemove.parent.children.indexOf(nodeToRemove);
      //   if (indexInChildren !== -1) {
      //     nodeToRemove.parent.children.splice(indexInChildren, 1);
      //   }
      // }

Just commented the last code, and it worked fine.