[SOLVED] Adding and removing layers dynamically?

I have entities for which the render component is added in the game, the problem is that I cannot add layers in the editor, layers must be added using code, how can I do it?

Entity has an empty _layers array, it is visible not only in the world layer but also in the minimap (although the camera is set to show only one layer)

Hi @Newbie_Coder,

You need to modify the layers array found on the render component, sample code in adding a new layer:

const layers = this.entity.render.layers;

const newLayer = this.app.scene.layers.getLayerByName('New Layer');

// --- check if the layers has already been added, if not add it
if(layers.indexOf(newLayer.id) === -1) layers.push(newLayer.id);

// --- it's important to set the layers array anew, don't modify the array directly
this.entity.render = layers;


1 Like

Works as expected with a few changes, many thanks!

1 Like

Hey @Newbie_Coder could you share the changes that made it work? I?m struggling with this one

The code @Leonidas provided should work without changes. Changes that were mentioned are likely related to exactly what was needed in a specific case, for example remove a layer, instead of add a layer.