Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Aug 18, 2017 at 03:32 AM by Ben_Huang4163 for the following reason:

Other

avatar image
0
Question by Ben_Huang4163 · Aug 16, 2017 at 02:39 PM · musicscene-changemanagement

how to changes music tracks along with scene changes?

Hi: i try to use a music manager to control music playing, like one music track matching one scene. i want it to turn the music from one to another automatically when i enter a new scene. i keep the music manager gameobject don't be destroyed between scenes by using singleton pattern, because i want it to control music volume. this is the code i tried, it doesn't work, it only keep playing the first track.

 public class AudioManager : MonoBehaviour {
 
     #region Singleton
     private static AudioManager _instance;
     public static AudioManager Instance
     {
         get
         {
             if (_instance==null)
             {
                 _instance = GameObject.FindObjectOfType<AudioManager>();
                 DontDestroyOnLoad(_instance.gameObject);
             }
             return _instance;
         }
     }
     #endregion
 
     AudioSource musicPlayer;
 
     public AudioClip Level01Music;
     public AudioClip Level02Music;
     public AudioClip Level03Music;
 
    
 
     private void Awake()
     {
         #region SingletonRun
         if (_instance==null)
         {
             _instance = this;
             DontDestroyOnLoad(this);
         }
         else
         {
             if ( this !=_instance)
             {
                 Destroy(this.gameObject);
             }
         }
         #endregion        
     }
 
 
     // Use this for initialization
     void Start () {
         musicPlayer = GetComponent<AudioSource>();
         levelMusicPlay();
     
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 
     void levelMusicPlay()
     {
         Scene currentScene = SceneManager.GetActiveScene();
         if (currentScene.name== "Level 01")
         {
             if (Level01Music != null)
             {
                 musicPlayer.clip = Level01Music;
                 musicPlayer.Play();
             }
             else
             {
                 Debug.LogWarning("Can not find level01Music");
             }
            
         }
 
         if (currentScene.name == "Level 02")
         {
             if (Level02Music != null)
             {
                 musicPlayer.clip = Level02Music;
                 musicPlayer.Play();
             }
             else
             {
                 Debug.LogWarning("Can not find level02Music");
             }
         }
         if (currentScene.name == "Level 03")
         {
             if (Level03Music != null)
             {
                 musicPlayer.clip = Level03Music;
                 musicPlayer.Play();
             }
             else
             {
                 Debug.LogWarning("Can not find level03Music");
             }
         }
        
     }
 }


is there any way to achieve my goal? Do an audiomixer will help? thank you.

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

1 Reply

  • Sort: 
avatar image
0

Answer by TheSOULDev · Aug 16, 2017 at 04:47 PM

Your music change script is only called the first time the script is loaded. Because you're not destroying the object carrying the script, it will never be reloaded, and Start() won't be called. I don't know why you don't want it to be destroyed as destroying it would fix your problem of the music not changing. If you want to keep the volume, you could save the state of the music settings, save a "launch parameters" file loaded upon a scene change which will determine what data you'll load in a new scene. Otherwise, you could get some other game object that is loaded on a new scene which will run musicPlayer.Play on start.

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

Follow this Question

Answers Answers and Comments

70 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

Related Questions

Destroy "Don'tDestroyOnLoad" script in a specific scene ?? 3 Answers

Music trackers and iOS 0 Answers

adding music to the main menu screen 0 Answers

.wav file not working 2 Answers

Large music files make asset importing slow / game large. Compression ideas? 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