Problems with Layers

Hi, I am trying to use a layer to show and hide an object (I need to do it with layers for reasons that are not important now).

The idea is to set a “myLayer” layer to an object, and add/remove this layer from the camera that renders the object. Altohugh the idea works with the editor, it does not work programatically doing this:

This is the way I create the object:

// this a function that creates a sphere
var myEntity = Primitives.addSphere(this._sceneRoot, 0.1, 0.1, 0.1);

var myLayer = this.app.scene.layers.getLayerByName("myLayer");
var newLayers = new Array(0);
newLayers.push(myLayer.id);

myEntity.model.layers = newLayers;
this._sceneRoot.addChild(myEntity);

This is how I try to enable the layer in the camera programatically (but I does not have any effect):

var myLayer = this.app.scene.layers.getLayerByName("myLayer");
this._myCamera.camera.layers.push(myLayer.id);
myLayer.addCamera(this._myCamera.camera);

I don’t understand why there are two ways of accessing the layer (via the layer and via the camera). It’s seems rather inconsistent… Do I need to set both? Only one? Which one??

And this is how I try to disable the layer in the camera:

var myLayer = this.app.scene.layers.getLayerByName("myLayer");
myLayer.removeCamera(this._myCamera.camera);

// This is a function I have to remove an item from an array        
var newLayers = Utils.removeFromArray(this.myLayer.camera.layers, myLayer.id);
this._myCamera.camera.layers = newLayers;

The “myLayer” layer exists in the project (I created it in the editor) so it does not need to be created with code.
The “myCamera” camera has initially the “myLayer” layer in its list of layers (I set it with the editor).

However the object is not initially visible, and there is no way to make it visible unless I add the layer 0 to the entity when I create the entity. Im that case, the code changes to this:

newLayers.push(0);
newLayers.push(myLayer.id);
myEntity.model.layers = newLayers;

Doing this, the object is shown (because it is in the “World” layer), BUT I CANNOT make it dissapaear removing the “myLayer” layer from the camera.

Any ideas?
As I said, I need to use layers. The question is that it seems to work differently from the editor than with code.

Thanks in advance.