[SOLVED] Enable/unable a selected script

Help please! I tried unable/enable the scripts of a Entity with …if(something)…this.entity.script.enabled=‘false’;… but this deactivates all the scripts of the entity … how could I focus on a particular script? Thanks coders :slight_smile:

1 Like

Okay, what are you trying to accomplish? There might be a better way of doing it, then using enabled/disabled function.

Thanks for your quick response. Okay. I try to toggle some scripts that now accompany my entity (deactivate one to activate another one.) Of course, doing it with the functions also sounds interesting. Could you give me an example please?

So basically if an event happens, change the script? If so, you could use this.app.scripts.get('ScriptName').enabled = value;

var ExampleScript = pc.createScript('exampleScript');

//Attributes
ExampleScript.attributes.add("num", { type: 'number', title: 'Number' });

var ThisNumber;
var ThatNumber;

// initialize code called once per entity
ExampleScript.prototype.initialize = function() {
    
};

// update code called every frame
ExampleScript.prototype.update = function(dt) {
    // If num equals ThisNumber, OtherScript is enabled Otherwise, if num equals ThatNumber, OtherScript is false.
    if (num === ThisNumber) {
        this.app.scripts.get('OtherScript').enabled = true;
    }  
    else if (num === ThatNumber) {
        this.app.scripts.get('OtherScript').enabled = false;
    }
};


// swap method called for script hot-reloading
// inherit your script state here
// ExampleScript.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

Using this.entity,.script affects all scripts attached to the entity. There might be a better way than what I’ve shown.

2 Likes

Solved. Works now for me

1 Like