Assigning Materials to Mesh on Click

Hi I am trying to change the material of a mesh using mouse onClick event. When clicked on the mesh, it only changes its color to white but no material gets assigned. Can you guys please point me in the right direction? I am using the desktop GUI and referring to materials already created using Material Viewer. The code I have used is below, Thank you.

    import { Scene, Color4, Mesh } from 'babylonjs';

    export default class MarbleDesk implements IScript {
        // Public members
        public blackColor = new Color4(0, 0, 0, 1);
        public BWTexture

         * Constructor
        constructor (public mesh: Mesh, public scene: Scene) {


         * Called once starting the script
        public start (): void {
            // You can access the scene everywhere
            this.scene.clearColor = this.blackColor;


            // You can access the attached object everywhere
            this.BWTexture = new BABYLON.StandardMaterial("floorfloor", scene);
             this.BWTexture = this.scene.getMaterialByName("wood");      
            //  this.BWTexture.diffuseTexture = new BABYLON.Texture("white_marble.png", scene);
            //  this.BWTexture.diffuseTexture.hasAlpha = false;
            //  this.BWTexture.backFaceCulling = false;
            this.BWTexture.diffuseTexture.uScale = 0.01;
            this.BWTexture.diffuseTexture.vScale = 0.01;


         * Called on each frame
        public update (deltaTimeMs: number): void {
            // Your code...
        scene.onPointerPick = function(evt, pickInfo) {
            if(pickInfo.hit) {
                if (pickInfo.pickedMesh != null){
                    mesh.material = this.BWTexture

         * Called once the attached object has been disposed
        public dispose (): void {
            // Called once the attached object has been disposed

    // Export the script as an attached script
    // (attached to a node or scene)

mesh should probably pickInfo.pickedMesh here and try creating the code in the playground, it will be much easier to troubleshoot.

Hey @sebavan thank you for your prompt response. I have changed the code accordingly but it still does it. I am thinking if that is something to do with how I am loading the texture? Since I am using the desktop version of Babylon.js, is there a specific way i should load the texture?

Currently I am trying to refer a material that is already created in the scene using GUI “Materials Viewer”. The only reason that I am using the desktop version is I find its easy to select and move objects around.

My project folder only has the scene.editporject file and a folder called “scene”. which has all the textures. I also pointed to “./scene/white_marble.png” and “/scene/white_marble.png” but did not work.


So I have changed the way I assign the material to the mesh as below,

pickInfo.pickedMesh.material = scene.getMaterialByName(“wood”)

Now the texture changes, but its a plain brown color. It should be like the material you see on the floor. Do I need to use any kind of render mechanism to load the material properly back to the mesh? See my gif below

As @sebavan said, you should provide a repro in the Playground (Introduction to the Playground - Babylon.js Documentation) for us to be able to help.