Change Material Coding error

Helo Guys, I cannot get the material to change when I press the orange button, I have used this same script in other projects and it always worked well. I have reviewed the object name in the script and it is correct. I don’t know what’s going on.

This is the message:

[Change%20Material_01.js?id=32706171&branchId=f2680fbc-d00d-41fc-9abb-474c27bf34dc:16]: Uncaught TypeError: Cannot read property ‘0’ of null

TypeError: Cannot read property ‘0’ of null
at scriptType. (https://launch.playcanvas.com/api/assets/files/Scripts/Change%20Material_01.js?id=32706171&branchId=f2680fbc-d00d-41fc-9abb-474c27bf34dc:16:82)
at ElementComponent.fire (https://code.playcanvas.com/playcanvas-stable.dbg.js:326:18)
at ElementInput._fireEvent (https://code.playcanvas.com/playcanvas-stable.dbg.js:53994:13)
at ElementInput._onElementMouseEvent (https://code.playcanvas.com/playcanvas-stable.dbg.js:53904:12)
at ElementInput._handleUp (https://code.playcanvas.com/playcanvas-stable.dbg.js:53750:9)

The script in the button is this

var ChangeMaterial01 = pc.createScript('changeMaterial01');

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

ChangeMaterial01.attributes.add('whiteMaterial', {
    type: 'asset',
    assetType: 'material'
});

// initialize code called once per entity
ChangeMaterial01.prototype.initialize = function() {
    this.entity.element.on('click', function (event) {
        if (this.app.root.findByName("Fuselaje tubo central").model.meshInstances[0].material === this.redMaterial.resource) {
            this.app.root.findByName("Fuselaje tubo central").model.meshInstances[0].material = this.whiteMaterial.resource;
        }
        else  {
            this.app.root.findByName("Fuselaje tubo central").model.meshInstances[0].material = this.redMaterial.resource;
        }
    }, this);
};

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

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

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

Thanks for your help

Looks like the error is saying that there is no meshInstances on the model component of ‘Fuselaje tubo central’

I would double check that that entity is is the correct one and is referencing an asset.

If you are still having problems, can you post a link to the project or at least a built version of the app to look at please?

@yaustar Yeap, The link https://playcanvas.com/editor/scene/949610

Project is private unfortunately. Can you either post a build or add yaustar with read permissions please?

What about using this.app.root.findByName("Fuselaje tubo central").model.model.meshInstances[0].material instead? Whenever I need access to meshInstances, I have to do entity.model.model.meshInstances. Would explain why the error is saying meshInstances is null.

1 Like