Problems with light stateChangeObservable

Hi All, it seems as though onEnabledStateChangedObservable does not exist on directional lights as documented? What am I doing wrong.

light = new DirectionalLight(‘someName’, new Vector3(0, -1, 0), scene);
let observer = light.onEnabledStateChangedObservable;

here observer is null.

observer.Add(function()=>{ … }));

and this results in an exception.

Can you give us a playground example please? That makes it much easier to help troubleshoot what might be happening

I put a playground together.

It seems to work fine for me this way:

Note that this observable only fires when the enabled state of the light changes. This so if the light is disabled it will fire, and if the light is then re-enabled it will fire.

You can test this by using the little “light” icon in the inspector of the light node:

Hope this helps.

If you ever want to change the state of a light in your code you can do so with the light.setEnabled() function.


thanks for the example. that exact code literally cut and paste results in “Cannot read properties of undefined (reading ‘add’)” . Am I crazy?

is this a babylon version issue?

“dependencies”: {
@babylonjs/core”: “^4.2.0”,
@babylonjs/inspector”: “^4.2.0”,
@babylonjs/loaders”: “^4.2.0”,
@babylonjs/materials”: “^4.2.0”,
@babylonjs/serializers”: “^4.2.0”,

That might be the case, the playground uses the most current version which is 5.0.0-alpha.59 current