How do you properly remove an object from the scene?
In this playground, a mesh and ground is created, then I attempt to use:
scene.removeMesh(sphere);
visually it looks like it works, but with the inspector open you can see that the sphere is actually still in the scene. So clearly it’s just removing the mesh and leaving some sort of parent node?
Seems like an easy way to achieve making the sphere dispose correctly is setting the action manager to be on the killBox rather than the sphere. The actionManager also has a dispose method, but calling that before calling sphere.dispose didn’t seem to work.
Found some advices to dispose meshes with null as well:
mesh.dispose();
mesh = null;
Is it necessary to tell GC that he can collect that garbage? I have a problem in typescript with such approach because it doesn’t allow to set mesh to null. I can do following:
mesh.dispose();
( mesh as any ) = null; // <-- this line
The method you want to use does depend on if you enumerate through “map”-object of your meshes, then using delete is prefered, because it will be undefined (inexistent) and your loops don’t consider the mesh anymore. Else if it has to be equal null, because you need this mesh as null-property in any of your evaluations afterwards. Else if scope is going to be left anyway, I would just use dispose(), but I’m new to TypeScript, aswell. So maybe someone can correct me if I’m wrong.
FWIW, if anyone else comes here - if you’re disposing of individual nodes in a scene, DO NOT iterate via scene.transformNodes.forEach() ! If you call transformNode.dispose() inside your iterator, it will modify the array out from under your iterator causing nodes to be skipped seemingly at random.
I ended up copying the array first with splice( 0 ), then iteration became stable despite nodes being disposed.
Yes I tried scene.removeTransformNode() instead - no help there.