[SOLVED] Change material of "Model (legacy)" with javascript


In our project we had to use the old “Model (legacy)” for some of the 3D objects in our scene. However, we wish to be able to change the material from javascript at runtime of an instance of this object.


I noticed there is something called “Entity materials” in the bottom right corner of the editor, which seems to support this feature, but how to do this from a script?

Hey, this is how you can access the entity material and assign any other material to it.


Thanks a bunch!

so how would I do it from a material that is being attributes ?
(i can’t get this to work for me…)

I am not sure what you mean. Can you share the issue in detail with the code snippet, please?

var PlayerAnimationHandler = pc.createScript('playerAnimationHandler');
PlayerAnimationHandler.attributes.add('mat_one', {
    type: 'asset'

PlayerAnimationHandler.attributes.add('Mat_two', {
    type: 'asset'
PlayerAnimationHandler.attributes.add('Mat_three', {
    type: 'asset'
PlayerAnimationHandler.prototype.initialize = function () {
var M = this.entity.model.model.meshInstances(2);
M.material = this.mat_one;

i finally got it to work…

ChangeTexture.attributes.add("materials", {
    type: "asset",
    assetType: "material",
    array: true,
    title: "List Of Materials",

ChangeTexture.attributes.add('currentMaterial', {type: 'number', default: 0});
ChangeTexture.prototype.SetMaterial = function()
    var material = this.entity.model.meshInstances[2].material = this.materials[this.currentMaterial].resource;
1 Like