[SOLVED] After calling the Destroy method of Entity, the Destroy method of the script component will not be invoked

// Script components overloaded destroy
myScript.prototype.destroy = function(name){
   console.log('script destroy');
}
// Entity add script components
var newEntity = new pc.Entity();
newEntity.addComponent('script');  
newEntity.script.create('myScript');
//Then entity calls destroy.
entity.destroy();

// results
'script destroy’does not print.
That is, the destroy method of myScript is not invoked.

What do you mean by print? I can’t see any console.logs or alerts in your code

That is, the destroy method of myScript is not invoked.

Hello - destroy is the name of the event fired on the script when it’s destroyed. So you need to register your destroy method as an event callback like so:

initialize () {
    this.on('destroy', this.destroy, this);
}
2 Likes

Thank you. I thought it was automatically invoked.

Thank you. I thought it was automatically invoked.