[SOLVED]Disable a Script Component Problem

Helloo dear Community,
Now I spend the whole evening to debug my code and finaly found the problem. Its easy but i dont get it.

I recreated the problem in a fresh “Basic Mouse Input” project-fork. And added just 2 lines of code;
this.entity.model.enabled=false;
and
this.entity.script.enabled=false;

https://playcanv.as/p/U9p9Z0av/

You will see that you can disable the Mesh-Model component with the left click but you can’t disable the Script component with the right click. Why is this so?

var Mouse = pc.createScript('mouse');

Mouse.attributes.add('redMaterial', {
    type: 'asset',
    assetType: 'material'
});

Mouse.attributes.add('greenMaterial', {
    type: 'asset',
    assetType: 'material'
});

Mouse.attributes.add('blueMaterial', {
    type: 'asset',
    assetType: 'material'
});

// initialize code called once per entity
Mouse.prototype.initialize = function() {
    this.pos = new pc.Vec3();
    
    // Disabling the context menu stops the browser displaying a menu when 
    // you right-click the page
    this.app.mouse.disableContextMenu();

    // Use the on() method to attach event handlers. 
    // The mouse object supports events on move, button down and 
    // up, and scroll wheel.
    this.app.mouse.on(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);
    this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
};

Mouse.prototype.onMouseMove = function (event) {
    // Use the camera component's screenToWorld function to convert the 
    // position of the mouse into a position in 3D space
    var depth = 10;
    var cameraEntity = this.app.root.findByName('Camera');
    cameraEntity.camera.screenToWorld(event.x, event.y, depth, this.pos);

    // Finally update the cube's world-space position
    this.entity.setPosition(this.pos);
};

Mouse.prototype.onMouseDown = function (event) {
    // If the left mouse button is pressed, change the cube color to red
    if (event.button === pc.MOUSEBUTTON_LEFT) {
        this.entity.model.meshInstances[0].material = this.redMaterial.resource;

        /////////////---------DISABLE MESH WORKS--------------//////////////////////////////
        this.entity.model.enabled=false;
    }

    // If the left mouse button is pressed, change the cube color to green
    if (event.button === pc.MOUSEBUTTON_MIDDLE) {
        this.entity.model.meshInstances[0].material = this.greenMaterial.resource;            
    }

    // If the left mouse button is pressed, change the cube color to blue
    if (event.button === pc.MOUSEBUTTON_RIGHT) {
        this.entity.model.meshInstances[0].material = this.blueMaterial.resource;

        ////////////-------DISABLE SCRIPT DONT WORK-------------//////////////////////////////
        this.entity.script.enabled=false;
    }
};

What are you expecting to see/happen when you disable this.entity.script?

Heyhey yaustar,

I expect when I disable the script-section the script “mouse” should be disabled too. So the cube should not be able to move or to change the color via middle-click etc.
Or do i miss something? :smiley:
screen

Yeah, that should happen AFAIK. Do you have a link to the project that people can take a closer look to please?

Yes sure here it is.

https://playcanvas.com/project/595490/overview/disable-script

Like i said its just a fresh fork of the “Basic Mouse Input” -Tutorial with just 2 new lines of code added.

I think I know what’s happening.

When you set enabled to false, it stops the updates being called in all the scripts attached to the script component.

However, it doesn’t do anything about event listeners. Therefore the mouse move and down events are still being processed by the move mouse script.

You can fix this by removing the event listeners on the script disable event and add them on the enable event.

ie: https://playcanvas.com/project/595661/overview/disable-script-from-forums

Oh…

Thanks a lot yaustar!