Follow Up to Playing Music Across Scenes, Need to Delete on Final Scene

@Joao_Dalvi and anyone else, I need to delete the music that has been playing across scenes. However, the way it is setup, currently, I cannot get the music that is playing to be destroyed when the final scene loads. Here is what I have tried:
void Awake()
{

        //When the scene loads it checks if there is an object called "MUSIC".
        musicPlayer = GameObject.Find("MUSIC");
        if (musicPlayer == null && !scene.Equals("Final"))
        {
            //If this object does not exist then it does the following:
            //1. Sets the object this script is attached to as the music player
            musicPlayer = this.gameObject;
            //2. Renames THIS object to "MUSIC" for next time
            musicPlayer.name = "MUSIC";
            //3. Tells THIS object not to die when changing scenes.
            DontDestroyOnLoad(musicPlayer);
        }
        else
        {
            if (this.gameObject.name != "MUSIC" || scene.Equals("Final"))
            {
                //If there WAS an object in the scene called "MUSIC" (because we have come back to
                //the scene where the music was started) then it just tells this object to 
                //destroy itself if this is not the original
                Destroy(this.gameObject);
            }
        }
    
} 

It doesn’t seem to work. I actually to play another short sound clip when this scene loads so I want to destroy this object so another can load.

Second Attempt:

        //When the scene loads it checks if there is an object called "MUSIC".
        musicPlayer = GameObject.Find("MUSIC");
        if (musicPlayer == null)
        {
            //If this object does not exist then it does the following:
            //1. Sets the object this script is attached to as the music player
            musicPlayer = this.gameObject;
            //2. Renames THIS object to "MUSIC" for next time
            musicPlayer.name = "MUSIC";
            //3. Tells THIS object not to die when changing scenes.
            if (scene.Equals("Final"))
            {
                DestroyObject(musicPlayer);
            } else
            {
                DontDestroyOnLoad(musicPlayer);
            }
            
        }
        else
        {
            if (this.gameObject.name != "MUSIC")
            {
                //If there WAS an object in the scene called "MUSIC" (because we have come back to
                //the scene where the music was started) then it just tells this object to 
                //destroy itself if this is not the original
                Destroy(this.gameObject);
            }
        }

Hey @Kingdomseed, how are you?

Can you paste the script within the music GameObject? It is an Persistent GameObject, right?
This script that you have shared is attached to which gameObject?

It is attached to my main camera game object where the music itself is attached.

EDIT: @Joao_Dalvi I just created a new game object called MusicManager and moved everything over to it instead of keeping it attached to the main camera.

OK,

It is better to keep it separated as you just did, the best way in my opinion is:

Since the music is a persistent gameObject, you will have to ask something else to see if the scene is the final scene under the void Start() method (unless you want to put it under Update, which is not a good habit), and then ask it to find the music player gameobject (I would do this in the levelmanager gameobject, if there is one), and then ask it to destroy the music gameObject.

something like this:

void Start()
{
if (scene.Equals("FINALS"))
{
GameObject music = GameObject.Find("MusicManager");
if (music)
{
Destroy(music);
}
}
}

can’t try it since I’m away from my Computer, but it may work, if it don’t I can take a look when I arrive home

Doesn’t seem to be working correctly. It’s not detecting that the Scene is “Final.” Its almost like the code isn’t running.

how about Scene.name = “FINALS”? (or “Final”, don’t know what is the name of the final scene)

Well the Scene name is Final. What is FINALS?

Well, I could swear that you had used this name before… Sorry about the craziness :joy: use “Final” then

Although, “FINALS” is so much more awesome than “Final” :joy:

Hahaha well I got it! I have to put it in the Update function, unless there is another function like “OnSceneLoad” or something like that. I needed to call the “name” property of the Scene, not the scene itself. Then your code works great @Joao_Dalvi

void Update()
{
    scene = SceneManager.GetActiveScene().name;
    Debug.Log("Scene Name is " + scene);
    if (scene == "Final")
    {
        Debug.Log("Scene Detected");
        DestroyObject(musicPlayer);
    }
}
1 Like

It is a bit heavy to call those methods on Update, you could call it under Start() on a non-persistent gameObject if you want. Sorry for not being able to help you more, I don’t recall most of the scene methods, and being away from my computer makes it harder to check it.

So a GameObjectin that scene just for destroying the music object? How would I direct the script to destroy the correct object?

The best option in my opinion would be to put it in the SceneManager (if you have one), but you could put it in any gameobject that you know that will be on the last scene and that it is not a persistent GameObject, just ask it to find the gameobject like this:

void Start()
{
    if (SceneManager.GetActiveScene().name == "Final")
    {
        GameObject music = GameObject.Find("MusicManager");
        if (music)
        {
            Destroy(music);
        }
    }
}
1 Like

Perfect! Came to a solution about the same time that is basically this. Thanks again @Joao_Dalvi

1 Like

Very good!!

It is good to avoid using methods under Update. Our Computers has strong CPUs, so sometimes is hard to see the difference in performance, but it can be a game killer if we try to run our games using low-performance mobile phones and etc.

1 Like

Privacy & Terms