Why do we need the Singleton code?

So my question is was this example project specific or just in general because it really didnt seem like it was necessary for the project because the music player was working the same before Ben put Another music player in the Level1 scene. If you just keep the musicplayer in the splash screen and it doesnt destroy itself what was the purpose of the code?

Hi Jayy,

When you load a scene, all objects in that scene get created. Assuming you have five scenes, and all of them contain a persistent MusicPlayer, you’ll end up with multiple persistent MusicPlayer objects in your scene when you loaded a few scenes. To avoid this, you make the class a “singleton”. The superfluous MusicPlayer objects will destroy themselves including their game object, which results in a pleasantly tidy Hierarchy.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms