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