Using ColorPicker value

TIP: when using the value from ColorPicker onValueChangedObservable, be sure to use “.copyFrom(value)” instead of “= value”.

I ran across what I thought was a bug with ColorPicker but learned the hard way.

A simplified version of the “bug” is here. Select a sphere, change color, select the other sphere and change the color again. Both spheres now change color from the same ColorPicker. Not what I expected.

The correct way is to use “.copyFrom(value)” instead of “= value”.

I expect the ColorPicker implementation is efficient in that it (likely) updates a single value object instead of allocating a new Color3 object any time the ColorPicker changes value.

It would be nice if this were documented in the ColorPicker or onValueChangedObservable API document.

1 Like

That’s a good comment. Babylon does that, as you correctly said, to efficiently provide the color object, without causing too much GC events. Could be argued that a single object being GCed is not an issue, but when it comes to hundreds of them at the same frame, it could cause a slight reduction in performance.

The same goes to doing this:

const mesh1 = .....
const mesh2 = .....

mesh2.position = mesh1.position;
// this will also change the position of mesh2
mesh1.position.x = 3;

If you think a comment about it would be helpful, it is always possible to add it :slight_smile: Do you want to contribute this? we love community PRs!

I’m happy to contribute when I have set up my system to do so. No rush on the recommended document update. I’ll contribute the recommended change when I am in a position to do so (which may be quite some time as I’m working on a “never been done before” :wink: 3d dice roller").