I have a simple game
the first time it plays smoothly, but reloading it a second time through the restart button in-game gives insane errors.
I know there is a lot of discussion over it and multiple threads before, but they are complex and really I can’t grab what the hell subscribing and unsubscribing.
I dont know about PC but in unity when every scene reloads it always opens as new, not any mess of this subscribing or unsubscribing, why is this in Playcanvas??
here is my scene
https://playcanvas.com/editor/project/1057274
@Nofil_khan Is this the error you are seeing?
If you click on the yellow link next to it you can see what code is having the issue.
Playing the sound.

This is just the one of many errors, I am experiencing as scene is being played second time.
How to and why to delete root of scene when switching be scenes and what is that in first place??
On line 18 of your countgears.js script you create an event.
If you for example change the scene you have to remove this event, otherwise there is still be listening to this event in the new scene which may cause the error(s).
Instead of only creating the event, you can do something like below to remove the event when needed.
this.app.on('coindetected',this.countTheCoins,this); // line 18
this.on('destroy', function() {
this.app.off('coindetected',this.countTheCoins,this);
});
3 Likes
Thanks Very helpful,How about I destroy root of scene,that would del everything right?
So nothing left and everytime fresh scene
No, that’s not correct. The root is already destroyed with your current way of scene changing. You still need to handle the events manually.
1 Like
With events you mean , every custom function I created?
Also where to put these destroy/off command,the point where scene is about to switch?
I mean this.app.on
(like line 18 I mentioned).
2 Likes
In the initialize function, after you create the event with this.app.on
(like my example above).
1 Like
This worked!!!
Thanks for helppppppppppp
1 Like