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 /
avatar image
0
Question by chudangkhanhsasuke · May 15, 2018 at 09:42 AM · audiosceneaudioclipscene-change

Playing music through complicated scene

i'm having a problem with background music . I have 4 scene ( Test 1, Test 2, Trainning 1, Trainning 2 ) . I created GameObject called : "Test Music" with Tag "Background Music", GameMusic Script, Audio : Test.ogg in Test 1 + 2 scene and "Trainning Music" with the same tag, same Script, Audio : Trainning.ogg in Trainning 1 + 2. So what i want to do is : When changing the scene from Test 1 to Test 2, Trainning 1 to Trainning 2, Test 2 to Trainning 1 and Trainning 1 to Test 2, The script will find object with tag "Background Music" and check : If GameObject in previous scene( Using DontDestroyOnLoad ) have the same name as GameObject in current scene, the script will destroy GameObject in current scene. If they have different name , the script will destroy the GameObject in previous scene. I know i can find a lot of way about how to do it, but it's not what i want to do. For Example : https://answers.unity.com/questions/1253516/playing-audio-through-multiple-scenes.html This answers almost like my purpose but it add scene in order to make the audio to be destroyed when change to some specific scene. But if i have 60 70 scene with different music, do i need to add all of the scene ? or if i set specific scene to play specific music (Like playing Test.ogg when load Test 1 + 2 scene) , can music continue to play when go to Test 2 from Test 1 ?

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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Harinezumi · May 15, 2018 at 12:25 PM

Is this what you want?

 // this will only be run for the game object in the newly loaded scene
 private void Awake () {
     GameObject[] backgroundMusics = GameObject.FindGameObjectsWithTag("Background Music");

     // note: if there are more than 2 objects found, you might end up without any background music!
     for (int i = 0; i < backgroundMusics.Length; ++i) {
         if (backgroundMusics[i] != gameObject) { // check that it is not itself
             if (backgroundMusics[i].name != name) { Destroy(backgroundMusics[i]); } // destroy background music with different name
             else { Destroy(gameObject); } // keep background music with same name
         }
     }
     DontDestroyOnLoad(gameObject);
 }

However, instead of this I would do it with a singleton background music object which is told what music to play in each scene. That is, you would have a background music object with the below script in each scene, and at the beginning of each scene call from a different script (a kind of scene initializer) BackgroundMusic.Instance.Play(<track for the scene>);. The script (might need):

 public class BackgroundMusic : MonoBehaviour {
 
     private BackgroundMusic instance = null;
     public BackgroundMusic Instance { get { return instance; } }
 
     private AudioSource audioSource;
     private AudioClip currentTrack;
 
     private void Awake () {
         if (instance == null) {
             instance = this;
             DontDestroyOnLoad(gameObject);
 
             audioSource = GetComponent<AudioSource>();
         }
         else if (instance != this) { Destroy(gameObject); }
     }
 
     public void PlayTrack (AudioClip track) {
         // change track if it is different
         if (currentTrack != track) {
             currentTrack = track;
             // start if changed and not null, stop if it is null
             if (audioSource.clip != null) { audioSource.Play(); }
             else { audioSource.Stop(); }
         }
     }
 
 }
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

120 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

Related Questions

How can I check the scene and play audio accordingly? 1 Answer

Play a multiple audio sources sequentially 2 Answers

www.getAudioClip() Bug: One Second being cut off MP3. 2 Answers

Is it possible to switch audio tracks in a video? 0 Answers

What are the advantages of stereo sound effects? 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