Enable script when an entity is disabled

Hi, I need for my script when an entity is disabled the script will be enabled.
Here’s my script:

var FlashlightController = pc.createScript(‘flashlightController’);

FlashlightController.prototype.initialize = function() {
this.light = this.entity.root.findByName(‘Player light’);
this.light.enabled = false;
this.enabled = false;
};

FlashlightController.prototype.update = function(dt) {
if (this.entity.root.findByName(‘Flashlight trigger’).enabled == false) {
this.enabled = true;
}

if (this.enabled && this.app.keyboard.isPressed(pc.KEY_F)) {
    if (!this.keyDown) {
        this.light.enabled = !this.light.enabled;
        this.keyDown = true;
    }
} else {
    this.keyDown = false;
}

};

Hi @Drake_Aurin,

Are you getting any error or unexpected behavior?

Hello! Im not getting any errors at all.
But I managed to fix my script by changing it with this:

var FlashlightController = pc.createScript('flashlightController');

FlashlightController.prototype.initialize = function() {
    this.light = this.entity.root.findByName('Player light');
    this.light.enabled = false;
    this.flashlightTrigger = this.entity.root.findByName('Flashlight trigger'); 
};

FlashlightController.prototype.update = function(dt) {
    if (!this.flashlightTrigger.enabled) {
        if (this.app.keyboard.isPressed(pc.KEY_F)) {
            if (!this.keyDown) {
                this.light.enabled = !this.light.enabled;
                this.keyDown = true;
            }
        } else {
            this.keyDown = false;
        }
    }
};
1 Like