How to exclude the starting scene from the singleton

So, in this lesson, Rick had us set up a singleton to have the music play across all the scenes without restarting.

What I want to do is have the music on the Start Menu be different from music on the other two scenes (Game and Game Over).

I thought by not having the MusicPlayer script attached to the Start Menu scene’s Music Player object would be enough to work the way I want. But then I found out since the Music Player isn’t deleting on load from the other scene, it just overlays both tracks together on the Start Menu when you return from the Game Over scene.

So, I was hoping there might be a way to say if the current scene is Start Menu then destroy the previous MusicPlayer game object rather than the one attached to the scene on load.

Not sure if that makes any sense. I’m really new to all this and not really sure the proper way of wording things but any help would be greatly appreciated!

Hi,

Actually, Rick implements a “singleton” to keep the “first” MusicPlayer and destroy others. There is a bug in his code, though, which he fixes in a later video.

Check if your “singleton” calls gameObject.SetActive(false); in the same if-block where Destroy(gameObject); gets called. If there isn’t that line of code, add it.

If you want to destroy the “previous” MusicPlayer, you somehow need to define what “previous” is. Not for me, of course, but for Unity. This can be done with a variable, in this case, a static variable.

Thanks for the heads up about the bug fix.

As for how I might achieve what I’m trying to do. Is it possible for you to give me a little more to go on without completely giving it away?

So, doing a quick search on static variables seem to suggest that they create a variable that can’t be instantiated. But I’m not really sure what to do with that information besides what you’ve mentioned it would be used for.

I could show you a different “singleton” implementation from another project. If you understand it, you’ll be able to modify it to make your idea work. At the moment, the “other” ScenePersist objects get destroyed with the code below. Figure out how “other” is defined.

Alternative "singleton"
public class ScenePersist : MonoBehaviour
{
    static ScenePersist instance = null;

    int startingSceneIndex;

    void Start()
    {
        if (instance == null)
        {
            instance = this;
            SceneManager.sceneLoaded += OnSceneLoaded;
            startingSceneIndex = SceneManager.GetActiveScene().buildIndex;
            DontDestroyOnLoad(gameObject);
        }
        else if (instance != this)
        {
            Destroy(gameObject);
        }
    }

    void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    {
        if (startingSceneIndex != SceneManager.GetActiveScene().buildIndex)
        {
            instance = null;
            SceneManager.sceneLoaded -= OnSceneLoaded;
            Destroy(gameObject);
        }
    }
}
1 Like

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

Privacy & Terms