[SOLVED] How to find if an entity has a particular script attached

Let’s say I am ray casting to an entity, and I want to be sure it has a specific script attached for instance ‘ChangeMaterials’

How would I go about it? I’ve tried this but it’s not working.

if(result.entity.script.has('ChangeMaterial')){
            console.log('Can Change Mat');
            result.entity.script.scripts[0].changeMat();
        } else {
            console.log('nope keep looking');
        }

I’ve used this instead and works fine, but this is prone to more user error, since I have to make sure everything is tagged correctly. It would be nice to just check if it has the script attached or not.

result.entity.tags.has('changeMat')
//instead of 
result.entity.script.has('ChangeMaterial')

Any thoughts?

Imagine I have an entity with a script component. I add a new script called my-script.js which initially looks like this:

var MyScript = pc.createScript('myScript');

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

// update code called every frame
MyScript.prototype.update = function(dt) {
    
};

Then, I can add a second script called check-for-script.js:

var CheckForScript = pc.createScript('checkForScript');

// initialize code called once per entity
CheckForScript.prototype.initialize = function() {
    if (this.entity.script.myScript) {
        console.log("Yep. It's got the script!");
    }
};

Simple as that. :slight_smile:

1 Like

HELL YEA WILL’S BACK IN THE HOUSE! :smiley:

Been a while haha, I really want playcanvas to kill it and show people how powerful this tool is.

I’ve been doing my homework and putting in the time. I won’t let you guys down I promise :wink:

Cheers,AJ