Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Tolufoyeh · Jan 29, 2015 at 09:54 PM · audiomenuscene-switching

Audio from scene 1 & 2 continues to play in scene 3 even though it was not added to scene 3.

Hi guys. I used the code below to get the solve the problem of the same song that is playing in scene 1 starting to play from the beginning in scene 2, instead of seamlessly continuing the song when I switch scenes. The song plays in all my menu scenes, regardless of my switching between scenes. But when I switch into the scene that has actual game play, the menu song plays alongside the game play song. The unity console warns that I have 2 audio listeners in my scene, even though I didn't add the audio game object from the menu scenes to the scene that has game play. The script is attached to empty game objects (with an audio listener and an audio source) in the menu scenes, but such a "MenuAudioGameObject" does not exist in the game play scene. Any clue on what I can do?

public class MenuAudio : MonoBehaviour { private static MenuAudio instance;

 void Awake()
 {
     if (instance != null && instance != this)
     {
         Destroy (this.gameObject);
         return;
     } 
     else
         instance = this;

     DontDestroyOnLoad (this.gameObject);
 }

 // Use this for initialization
 void Start () 
 {
     if (!instance.audio.isPlaying)
                     audio.Play ();
 }

 public void TurnMusicOff ()
 {
     if (instance != null)
     {
         Destroy(this.gameObject);
         instance = null;
     }
 }

 void OnApplicationQuit()
 {
     instance = null;
 }

}

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
0
Wiki

Answer by yashpal · Jan 30, 2015 at 09:20 AM

@Tolufoyeh, basically you have two problems (1) you get the warning "you have 2 audio listeners in scene." and (2) music play in all the scenes.

In every scene camera have audio listener and your audio gameObject "MenuAudioGameObject" also have audio listener. so unity show warning "you have 2 audio listeners in scene.". this happens because your "MenuAudioGameObject" don't destroy in between scene.

so when you change the menuScene to gamePlayScene you turn off your audio Listener of "MenuAudioGameObject" and pause audioSource.

Comment
Add comment · Show 4 · 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 Tolufoyeh · Jan 30, 2015 at 10:52 AM 0
Share

Thanks! I just had to change the first line of the above code from private static $$anonymous$$enuAudio instance; to public static $$anonymous$$enuAudio instance; so I could call add this line to the start method of my GamePlayScene: $$anonymous$$enuAudio.instance.Turn$$anonymous$$usicOff (); and it works perfectly now. I thought the music might not resume when I switched from gameplay back to menu scenes but it all works like it should now.

avatar image Tolufoyeh · Jan 30, 2015 at 12:03 PM 0
Share

How do I mark the thread as solved? Or is that the job of the ad$$anonymous$$s?

avatar image meat5000 ♦ · Jan 30, 2015 at 12:23 PM 0
Share

Click the tick

avatar image Priyanshu · Jan 30, 2015 at 12:41 PM 0
Share

alt text

27269-howtotickananswer.png (2.0 kB)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Menu music stops playing after returning from game level 0 Answers

Play Sound On Object In Another Scene With Button from First Scene 1 Answer

AudioSource not playig after switching between scenes 0 Answers

Audio and Scene Switching 2 Answers

How to make menu which is able to list audio files, then load chosen one into the AudioSource in the scene 1 Answer


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