[SOLVED]How do I destroy an object with its child generation?

Hi, what I am trying to do is exactly this;

Let’s say I have an object named “Mother” and it has a child object named “child” attached to that object.

A script is defined in the “child” object and I want it to do the following function, when an external object interacts with the “child” object, I want it to destroy the “Mother” object and all the “child” objects attached to the “Mother” object.

I guess I need a code structure like “this.entity.Mom.destroy();”. How can I code this?

sds

Hi @Onur_Ozturk,

There are a few ways you could approach this. Do the number of levels in your hierarchy always remain constant? Then you could use this, though I would not recommend this approach as it is essentially hardcoding. What would be a better approach is to have a script on the parent entity, or the ‘mother’ entity as you call it, and trigger an event to destroy it when necessary. Have a look at this guide on script communication for that. The final approach I could think of would be to use script attributes and select the ‘mother’ entity for the ‘child’ entity in the editor while parsing the script. That can be done like so(make sure you set attribute type to entity).

Hope this helps!

2 Likes

Hello, thank you for your reply. The objects in the interface are not fixed, they are constantly formed with the same name during the work in the object structure that I have given visually above, so I guess I could not find the information I was looking for in the links you sent.

The “triggerPlane” script structure is like this. After some operations, I want to destroy the object to which the script is attached, together with its parent. Since this parent was created more than 1 time at runtime, I did not know how to access the parent directly touched by the Player object.

triggerPlane Script:

var TriggerPlane = pc.createScript('triggerPlane');

TriggerPlane.prototype.initialize = function () {
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
};

TriggerPlane.prototype.update = function (dt) {

};


TriggerPlane.prototype.onTriggerEnter = function (triggerEntity) {


    if (triggerEntity.tags.has('player')) {
        triggerEntity.script.controller.count++;
        triggerEntity.script.controller.countText.element.text = + triggerEntity.script.controller.count;

        if(triggerEntity.script.controller.speed<=100){
             triggerEntity.script.controller.speed+= 0.1;
        }        
        setTimeout(() => {
            this.entity.destroy();
        }, 0);
    }
};

In that case, perhaps you could assign a certain attribute to the parent on generation(a tag would work), then check if this parent contains the child entity. If these conditions are fulfilled, you can call the script instance of the parent entity that destroys it. For example, if the conditions are met, then you can do something like this, assuming the parent entity(currently contained in a local variable called parent) has a script called destroyMe.js with a method destroyEnt that destroys the parent -

parent.script.destroyMe.destroyEnt();.

Please forgive any formatting errors as I am currently on mobile.

2 Likes

Thank you, I will try your suggestion.

1 Like