Material of chaos
This is my code.
Only the last loaded material is not messy
The previously loaded material is overwritten

Hi @gyk !

I took a look at your project and seem to have been able to get the materials working in the desired way:

I would like to note that it was a bit difficult to comb through your code because of the variable names changing between functions whenever referring to this or it’s properties.

You may want to consider reviewing this documentation for additional examples of how to handle scope and how variables act and change between scopes:

The most important change that I made was to line 73 here:

my_entity.render.material = tempMat;

Whenever you are assigning a material to a render entity, each mesh that is assigned to that render component can have a different material, so you have to tell playcanvas which meshInstance’s material you want to change. I did it by modifying this line to read:

my_entity.render.meshInstances[0].material = tempMat;

Note that this code only works for render components with a single mesh. If there are multiple meshes, you will have to dive into them and appropriately select the correct one to modify.

You can view my fork of your project here:

You can also look at this example project for a more in-depth look at how you might handle a render component with multiple meshes here:

I hope this is helpful.

1 Like