How Can i Destroy The Music After DontDestroyOnLoad Between Two Scenes?

Hello everyone, I want to play the music between MainMenu and Upgrade scenes. When i open other scene, i want to destroy the music. The following codes did not work. Could you help me, please?

public GameObject music;

    void Awake()
    {

        if (SceneManager.GetActiveScene ().name == ("MainMenu") || SceneManager.GetActiveScene ().name == ("Upgrade"))  {
            DontDestroyOnLoad (music);
        } else
        {
            Destroy (music);
        }
    }

USE CODE TAGS

Awake runs once for every object, so that wont be called when a new scene is loaded. You’re looking for sceneLoaded, which you can use to add a delegate for when a new scene is loaded.

So you code would look something like this:

void Awake()
{
    DontDestroyOnLoad (music);
    // add the callback method when scene loads
    SceneManagement.sceneLoaded += OnSceneLoad;
}

// called when scene loads
void OnSceneLoad(Scene scene, LoadSceneMode mode)
{
    if(scene.buildIndex != indexOfMenuScene || scene.buildIndex != indexOfUpgradeScene)
    {
        Destroy(music);
    }
}
1 Like

Thank you so much. It is working :slight_smile:

  public GameObject music;

    void Awake()
    {

        DontDestroyOnLoad (music);

        SceneManager.sceneLoaded += OnSceneLoaded;
    }

    void OnSceneLoaded (Scene scene, LoadSceneMode mode)
    {
        if((scene.buildIndex != 0) && (scene.buildIndex != 1))
        {
            Destroy(music);
        }
    }

Great, but click the link at the top of my first post. No one will read your code if it isn’t formatted.

1 Like

Ah I see you reformatted it. Nice :slight_smile:

  • {
    • SceneManager.sceneLoaded += OnSceneLoaded;
  • }
    • void OnSceneLoaded (Scene scene, LoadSceneMode mode)
  • {
  • if((scene.buildIndex != 0) && (scene.buildIndex != 1))
  • {
  • Destroy(music);
  • }
  • }

hello sir, when can i put this code?
-thanks in advance!

  • public GameObject music;
  • void Awake()
  • {
  • DontDestroyOnLoad (music);
  • SceneManager.sceneLoaded += OnSceneLoaded;
  • }
  • void OnSceneLoaded (Scene scene, LoadSceneMode mode)
  • {
  • if((scene.buildIndex != 0) && (scene.buildIndex != 1))
  • {
  • Destroy(music);
  • }
  • }

hello sir, when can i put this code?
-thanks in advance!

@UnityEdwardPogii Uhhh… are you asking where that code is supposed to go? If so I HIGHLY recommend you watch the Unity scripting tutorials before diving any further into coding.

In any case, that code goes inside a C# class that you create in the editor, which goes on a singleton object created when the game starts.

Hi people,
I have an audio button in my game for audio on/off. When I am playing the game and if turned the audio off and return to main menu to play new game. When I start the game my audio is still off. I want a solution where if i return to main menu scene the audio object must destroy/reset in some way that when I start the game again the music start playing.
Thanks.