How to remove All child Entity?

Hi Guys. I trying to remove all child entity, but it has a problem.


If you watch the video, only a few items are deleted.

my algorithm is.

  1. Remove all child entity under parent entity.
  2. Add new children in parent entity.

my code is.

    if(contentView.children.length > 0) {
        contentView.children.forEach((e) => {
            console.log(contentView.children.length);
            e.destroy();
        });
    }

    if(this.itemData != null && contentView.children.length === 0) {
        var controller = this.storeControllerEntity;
        for (var idx = 0; idx < this.itemData.length; idx++) {
            controller.script.storeController._onAddStoreItem(this.itemData[idx].name, this.itemData[idx].price, this.itemData[idx].icon);
        }
    }

Please, Give me tips or links or answers.

Hi @erickim,

To delete entities using the children entity property as iterator works only by looping through it in reverse order (last to first). Otherwise each time you delete an entity the children list changes, so the loop will miss some entities.

for (var i = contentView.children.length - 1; i >= 0; i--) {
   contentView.children[i].destory();
}
1 Like

Thank you @Leonidas .
Your answer save my life. I wish you always be happy. :smiley:

1 Like