Color3.FromHexString is not a constructor

Hi All,

I am trying out babylonjs and my requirement is to change the color of a mesh dynamically based on what value I pass. I created a group of radio buttons and a handler function. This handler function receives the value of the color and assign a new color to the mesh.

My problem is that whenever I use the FromHexString() method to create a color, I get the following error:

Uncaught TypeError: babylonjs_core__WEBPACK_IMPORTED_MODULE_0_.Color3.FromHexString is not a constructor

I am using react and @babylonjs/core npm package

Here is a playground of the basic code structure. It wont execute as it is in react but still it shows how I am using the function

Color3 Error | Babylon.js Playground (babylonjs-playground.com)

I am a complete beginner and I dont know if I am missing something here

Thank you

Hi and welcome to the Community.
I don’t do react but I think you must be missing the namespace in there.
I would try with:

let newColor = new BABYLON.Color3.FromHexString(“#F0F0F0”)

or even something like that may be:

    var newcolor = item.hexString;
    let newColor = new BABYLON.Color3.FromHexString(newcolor);

Hopefully one of the above will work or else the string you are passing through might just not be a string or the parameter on your item is not read?
Just a guess…

1 Like

its a static function so dont use “new”

so this :

let newColor = new Color3.FromHexString(item.hexString);

should be

let newColor = Color3.FromHexString(item.hexString);

3 Likes

@shaderbytes This worked like a charm. Thank you so much. And thank you @mawa for your suggestion too :smiley:

1 Like