Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mohoruto · Jan 12, 2021 at 06:44 PM · audiomusicdontdestroyonloaddisable objectdisabling

How to STOP music when loading a SPECIFIC SCENE?

Hi there - I have background music on my main menu, which continues playing through each menu. However, when I load my first level, I want this music to stop. I have tried a few methods of solving this, but haven't been able to get a working fix. Any help would be hugely appreciated! :D

Here's my code:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;

public class MusicPlay : MonoBehaviour { public static MusicPlay musicplay; public GameObject music;

 void Awake()
 {
     if (musicplay == null)
     {
         DontDestroyOnLoad(gameObject);
         musicplay = this;
     }
     else if (musicplay != this)
     {
         Destroy(gameObject);
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Llama_w_2Ls · Jan 12, 2021 at 07:00 PM

In the update of this script, or preferably in the Start method of another script, you could destroy the music object when entering a specific scene. For example:

 void Update()
 {
     Scene currentScene = SceneManager.GetActiveScene();
 
     if (currentScene.name == "Level1")
     {
         // Stops playing music in level 1 scene
         Destroy(gameObject);
     }
 }

@Mohoruto

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Um4yr · Jul 29, 2021 at 09:02 PM

@Llama_w_2Ls i just have this, how do i make it stop playing this music when it switched scenes?

alt text


sc.png (52.2 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Bunny83 · Jul 30, 2021 at 08:18 AM

Don't use scene names in a script that is attached to an object in a different scene. Since your "MusicPlay" script is a singleton like object, you only have to add another tiny script which actually does the "stopping" or "starting" when the scene is loaded. For example

 // BackgroundMusicHandler.cs
 using UnityEngine
 
 public class BackgroundMusicHandler : MonoBehaviour
 {
     public enum Mode {EnsurePlaying, Restart, Stop};
     public Mode mode;
     void Start()
     {
         if (mode == Mode.EnsurePlaying)
             MusicPlay.musicplay.StartBackgroundMusic(false);
         else if (mode == Mode.Restart)
             MusicPlay.musicplay.StartBackgroundMusic(true);
         else if (mode == Mode.Stop)
             MusicPlay.musicplay.StopBackgroundMusic();
     }
 }


In your "MusicPlay" script you should add those two mentioned methods. Your MusicPlay script should probably cache the AudioSource that is attached to the MusicPlay script so it can easily access the audio source. So it would look something like this:

 public class MusicPlay : MonoBehaviour
 {
     public static MusicPlay musicplay;
     // either set backgroundMusic in the inspector or use GetComponent in Awake to initialize this variable
     public AudioSource backgroundMusic;
 
      void Awake()
      {
          if (musicplay == null)
          {
              DontDestroyOnLoad(gameObject);
              musicplay = this;
          }
          else if (musicplay != this)
          {
              Destroy(gameObject);
          }
      }
     public void StartBackgroundMusic(bool aRestart)
     {
         if (!backgroundMusic.isPlaying || aRestart)
             backgroundMusic.Play();
     }
     public void StartBackgroundMusic()
     {
         backgroundMusic.Stop();
     }
 } 

Now you can simply attach the "BackgroundMusicHandler" to any object in each scene and decide if you want to play background music, if you want to restart it or if you want to stop it.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

148 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Don't destroy a variable when changing scene 2 Answers

How to stop audio in a scene when DontDestroyOnLoad was already called? 1 Answer

How can I loop a song from a specific point? 1 Answer

Changing Continuous Audio Between Scenes 0 Answers

prevent unity from cutting phone music off? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges