#1180·engine

Error when destroying a child in initialize

Author: vkalpiasCreated Apr 24, 2018Updated Sep 26, 2026

To reproduce setup an Entity in the Editor like so:

Parent
 - Child1
 - Child2

Disable the Parent. Add a script to Child1 that destroys it in initialize like so:

TestScript.prototype.initialize = function () { this.entity.destroy(); }

Create another script to clone the parent like so:

var clone = Parent.clone();
clone.enabled = true;
this.app.root.addChild(clone);

Notice there is an error thrown because in pc.Entity#_notifyHierarchyStateChanged we loop through the children of an entity and since we destroy one of its children that loop is invalid.