• 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 NatsuSuika · Feb 08, 2021 at 10:29 PM · game

How do I stop music already playing when entering a new Scene,How to stop Music from playing after Loading a new Scene?

I have a game im trying to code in Unity3d. I already have a DontDestroyOnLoad function running to play a music when the game loads into the main menu. But lets say I want to play a different song in game, how would I go about doing this? This is my music playing script.

 using UnityEngine;
 
 public class MusicManager : MonoBehaviour
 {
     private static MusicManager musicManagerInstance;
 
     // Start is called before the first frame update
     void Awake()
     {
         DontDestroyOnLoad(this);
 
         if(musicManagerInstance == null){
             musicManagerInstance = this;
         }
         else{
             Destroy(gameObject);
         }
     }
 
 }








Comment
Add comment · Show 1
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 Jinkel92 · Feb 09, 2021 at 01:46 AM 0
Share

Are you using Audio Source Component or is this a "Custom Audio Source"?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by RodrigoAbreu · Feb 09, 2021 at 05:33 AM

Hey @NatsuSuika, hajime mashite! Ideally you should have 2 AudioSources to do it properly, so you can fadeout the one playing and fadein the new one.

The Awake above on @ChocoboyYT solution will not be called multiple times (on each scene), it's only called the first time when the object is created (scene 1). You need instead to listen to the scenes loading.

  using UnityEngine;
  using System.Collections;
  using UnityEngine.SceneManagement;
   
  public class MusicManager : MonoBehaviour
  {
     private static MusicManager musicManagerInstance;
     public AudioClip landSong;
     public AudioClip watersong;
 
     private AudioSource audioSource;
 
     void Awake()
     {
         DontDestroyOnLoad(gameObject);
         
         audioSource = GetComponent<AudioSource>();
         
         Scene level = SceneManager.GetActiveScene();
 
         PlaySongBySceneName(level.name);
     }
     
     void OnEnable()
     {
         SceneManager.sceneLoaded += OnSceneLoaded;
     }
     
     private void OnSceneLoaded(Scene scene, LoadSceneMode mode
     {
         PlaySongBySceneName(scene.name);
     }
     
     private void PlaySongBySceneName(string levelname)
     {
         switch(levelname)
         {
             case "landScene":
                 audioSource.clip = landSong;
             break;
             case "waterScene":
                 audioSource.clip = waterSong;
             break;
             default:
                 // Do whatever
             break;
         }
         
         audioSource.Play();
     }
  }

Comment
Add comment · Show 7 · 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 ChocoboyYT · Feb 09, 2021 at 05:47 AM 0
Share

@RodrigoAbreu I see, excuse my lack of experience. I have been using Unity for a while, but I am in no way an expert

avatar image RodrigoAbreu ChocoboyYT · Feb 09, 2021 at 05:52 AM 0
Share

Nah man, I used most of what you suggested. Just got the Awake thing because I faced that some times. We're all always learning here ;) + I did not test it either

avatar image NatsuSuika RodrigoAbreu · Feb 09, 2021 at 10:49 AM 0
Share

It gives me the error Assets\Scripts\$$anonymous$$usic$$anonymous$$anager.cs(49,22): error CS1061: 'AudioSource' does not contain a definition for 'play' and no accessible extension method 'play' accepting a first argument of type 'AudioSource' could be found (are you missing a using directive or an assembly reference?)

Show more comments
avatar image
0

Answer by ChocoboyYT · Feb 09, 2021 at 05:22 AM

I think that you should do something like this

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
  
 public class MusicManager : MonoBehaviour
 {
       private static MusicManager musicManagerInstance;
       public AudioClip landSong;
       public AudioClip watersong;

       
      // Start is called before the first frame update
      void Awake()
      {
          AudioSource audioSource = GetComponent<AudioSource>();
          Scene level = SceneManager.GetActiveScene();
          string levelName = level.name;
          DontDestroyOnLoad(this);
  
          if(musicManagerInstance == null){
              musicManagerInstance = this;
          }
          else{
              Destroy(gameObject);
          }
          switch(levelname)
          {
                case "landScene":
                audioSource.clip = landSong;
                break;
                case "waterScene":
                audioSource.clip = waterSong;
                break;
                default:
                break;
          }
          audioSource.play();
      }
 }

This just checks the scene's name and plays the corresponding song. I'm not sure if it will work as I have not tested 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

138 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

Related Questions

getting udp package info inside unity (GlovePIE) 0 Answers

saving scene and game objects 1 Answer

Creating Fps Game Menu 2 Answers

How to create a Game Tree data structure in Unity. 0 Answers

After Building game how to turn it into a Running game 2 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