[SOLVED] How Do I Change A Models Material?

I am making a multi player game but i cant seem to figure out how to change the skin or material of the new players that join. I was reading around and i see that the following should change the material of a model. ANd just a fyi the data.skin works its a string called rain. and the material asset is in my game.
https://playcanvas.com/editor/scene/1391191

    newPlayer.model.meshInstances[0].material = this.app.root.findByName(data.skin);

i have no clue why this wont work. I keep getting the error blendType

Your problem is that you are using app.root.findByName, which return an entity.

So, you are trying to apply an entity to a material.

1 Like

Below an example project about how to change materials at runtime.

https://developer.playcanvas.com/en/tutorials/switching-materials-at-runtime/

1 Like

Thank you so much Albertos. I finally got it working with the following.

Network.prototype.changeSkin=function(data,np){
var rain = this.materials[1];
var self = np;
var pb = this.materials[0];
var chosen = null;
if(data.skin=="rain"){
    chosen = rain;
}else if (data.skin=="pb"){
    chosen = pb;
}
var allMeshInstances = [];
var renders = self.findComponents('render');

for (i = 0; i < renders.length; ++i) {
    var meshInstances = renders[i].meshInstances;
    for (var j = 0; j < meshInstances.length; j++) {
        allMeshInstances.push(meshInstances[j]);
    }
}      

for (i = 0; i < allMeshInstances.length; ++i) { 
    var mesh = allMeshInstances[i];
    mesh.material = chosen.resource;
}
};

I didnt want to use one of thos script asset things but i have to apparently.

2 Likes