Bug when calling .destroy() on SceneObject instantiated from Prefab
Hey guys,
I've noticed inconsistent behavior when calling .destroy() on Scene Objects. These Scene Objects are instantiated from a Prefab using scripting. In some cases, the Scene Object is removed, in others it's not. When resetting the lens, it's just a coin flip wether the Scene Objects will be deleted or not.
I'm developing a platform game (based on Doodle Jump). In this game, a player jumps from platform to platform. These platforms are generated based on the player's Y position. To keep the game running smoothly, platforms are garbage collected when out of screen, so there won't be too much objects to check for collisions. My platform prefab is just a really simple Scene Object with a Mesh Component (plane) to render it.
This is the abstracted code I'm using to delete the Scene Object:
const sceneObject = .. // the instantiated scene object
const delete = () => {
const deleteHandler = script.createEvent('DelayedCallbackEvent');
deleteHandler.bind(() => {
try{
sceneObject.destroy();
delete();
} catch (e) {
// there's an error so the object should be deleted
// print('enabling this print raises the odds of deleting the Scene Object..');
}
});
deleteHandler.reset(0);
};
delete();
As you see, I'm trying to create a loop to check wether the Scene Object is gone using a try catch. sceneObject.getTransform(); throws an error if sceneObject doesn't exist. If it passes that test, it should trigger the delete function again as this.sceneObject should be Null.
For some reason, adding a print to the catch increases the success rate of the code. Another weird thing is that the code is either working a 100% or 0%. Never is it just removing some of the platforms.
Tom
After a few hours of debugging, I found the bug in my code that caused this bug.
The level manager that was responsible for generating the platforms around the player has a reset method that was called twice in a row. This was causing some weird reference bugs that were really hard to trace.
Hope this will help someone with a similar problem!