Remove CSS2DObject not working

I need to remove all the CSS2DObject from my scene with below code it removes only first object then displays an error in traverse

scene.traverse(function(child){
                 if(child instanceof CSS2DObject){
                     scene.remove(child);
                 }
             })

what is the proper way to remove all CSS2DObject from scene

Duplicate of Cannot read property 'traverse' of undefined when traverse object in scene and remove it

1 Like
scene.children.forEach(function(){
                console.log(scene.children);
                
                if(scene.children instanceof CSS2DObject){
                    scene.remove(scene.children);
                }
            })

I tried this code also but didn’t work is this right way?

Try making an array of objects to remove, then do the removing outside of the scene object

const toremove = [ ];
scene.traverse(function(child){
if(child instanceof CSS2DObject){
toremove.push(child)
}
});
toremove.forEach(child => scene.remove(child));

2 Likes

Thanks buddy for help