How to remove previous mesh after each click

I used The code which remove the mesh after each click and
I Create a variable to store the reference to the previous mesh but nothing change
here is the code used :slight_smile:

let previousMesh=true;
window.addEventListener('click', handleClick);function handleClick() {
  // Remove previous mesh if it exists
  if (!previousMesh) {
    scene.remove(previousMesh);
  }

  // Create a new mesh
  const geometry = new THREE.BoxGeometry(5, 6, 1);
  const material = new THREE.MeshBasicMaterial({ color: 0x00ff00,wireframe:true });
  const mesh = new THREE.Mesh(geometry, material);

  // Add the new mesh to the scene
  scene.add(mesh);

  // Update the reference to the previous mesh
  previousMesh = mesh;
}

Is somehow a mishmash.

let previousMesh=true;   // Boolean data type

should be any mesh

previousMesh = mesh;  // three mesh

try without !

if ( previousMesh) {
    scene.remove(previousMesh);
  }

Donโ€™t forget to dispose the geometry.

In my current project, Iโ€™m doing this for a number of meshes:
( geo[ i ] belongs to mesh[ i ] )


    for ( let i = 0; i <= apdCount; i ++ ) {
 	
        if ( mesh[ i ] ) {
                
                sceneA.remove( mesh[ i ] );
                geo[ i ].dispose( );
                
        }
        
    }