Material not updating the texture in script

The following code changes the material’s texture

//usage
this.entity.model.material.diffuseMap = this.tex_lens.resource;
this.entity.model.material.update();

But the below code gives error as : material.update is not a function

// declaration
Script.attributes.add('mat_lens', {type: 'asset', assetType: 'material',default: null });
//usage
this.mat_lens.diffuseMap = this.tex_lens.resource;// asset.resource;
this.mat_lens.update();

Everytime if we have to take the object and then change the material properties then it becomes longer code. Is there anything wrong in the second code?

Asset attributes are referring to the pc.Asset not the resource itself. So in this case you should do

this.mat_lens.resource to access the material because this.mat_lens is the pc.Asset.

1 Like

Thank you Vaios. Only problem is many such important things are not easily available. Can you share some links.

You can find more information about script attributes in the User Manual here:

http://developer.playcanvas.com/en/user-manual/scripting/script-attributes/

And also in the API reference here:

http://developer.playcanvas.com/en/api/pc.ScriptAttributes.html

Let us know if you need any more help :slight_smile:

Thank you again.
Please help on the below query as well.