Reputation: 1249
I have a cube that I'd like to change the color of through code. I'm attempting to use the setProperty() method. When I put the following code in an update loop and attach it to a cube that I've made blue it stays blue, but the console says that it is black (0). When I remove the setProperty() line then I get the color value from the editor.
this.getEntity().setProperty("diffuseColor",0);
console.log(this.getEntity().getProperty('diffuseColor'));
Upvotes: 0
Views: 47
Reputation: 142
How I've done it is referencing the material using the Asset Registry, then calling the setProperty method on the variable referencing it, like this:
var material = this.getAssetRegistry().getAssetByName("NameOfMaterialInEditor");
material.setProperty("diffuseColor", 0x000000);
You can also use the attribute system to reference the material by creating it in the Code Editor then setting it on the component in the Studio, then whatever you named the attribute can be referenced programmatically with
this.nameOfAttribute.setProperty("diffuseColor", 0x000000);
Upvotes: 1
Reputation: 81
That code should work fine (and it seems to be working for me). diffuseColor
is a property of materials though so make sure that you're setting this property on the MaterialAsset and not your mesh.
If you want to only change the color of the material on one of your meshes, you'll need to create a new material for it (or clone the existing one).
Hope that helps.
Upvotes: 1