How can I set a material using the script?

I attached a script to some entity. It includes this attribute:

Person.attributes.add('material_happy', {type: "asset", assetType: "material"});

I droped a material into that attribute in the editor.
Then somewhere in the script I want to change the material to this ‘material_happy’. I tried as follow:

this.entity.render.material = this.material_happy;

But that gives a flood of errors:

ASSERT FAILED:

[[playcanvas-1.58.2.dbg.js:0]](https://code.playcanvas.com/playcanvas-1.58.2.dbg.js): Script error.

ASSERT FAILED:

[[playcanvas-1.58.2.dbg.js:0]](https://code.playcanvas.com/playcanvas-1.58.2.dbg.js): Script error.

ASSERT FAILED:

[[playcanvas-1.58.2.dbg.js:0]](https://code.playcanvas.com/playcanvas-1.58.2.dbg.js): Script error.

ASSERT FAILED:

[[playcanvas-1.58.2.dbg.js:0]](https://code.playcanvas.com/playcanvas-1.58.2.dbg.js): Script error.

ASSERT FAILED:

[[playcanvas-1.58.2.dbg.js:0]](https://code.playcanvas.com/playcanvas-1.58.2.dbg.js): Script error.

ASSERT FAILED:

[[playcanvas-1.58.2.dbg.js:0]](https://code.playcanvas.com/playcanvas-1.58.2.dbg.js): Script error.

Too many logs. Open the browser console to see more details.

What am I doing wrong?

this.entity.render.material = this.material_happy.resource;

pretty sure thats how its done

1 Like

Yes!, that did the job. Thx!

1 Like