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
1
Question by L1vD3v · May 05, 2014 at 05:15 PM · javascriptduplicateaudio listenerprevent

Prevent audio listener from duplicating

I have made a script, which makes a game object not destroy if a boolean is true or destroys if the boolean is false. Then on this game object I have my "menu song" playing, then when I load a "game scene" I have gotten another script which turns the boolean to false so that the "menu music" isn't playing. Now my problem is that, I need to have the audio listener on the game object with the audio in it to here the music, but then because of the "DontDestroyOnLoad" the audio listener duplicates every time I go from "second menu scene" to the "first menu scene". How would I prevent the audio listener from duplicating?

Here is my first script.

 static var MenuMusicPlaying : boolean = true;
 
 function Awake() {
     
     if (MenuMusicPlaying == true) {
     DontDestroyOnLoad(gameObject);
     
     } else if (MenuMusicPlaying == false) {
     Destroy(gameObject);
     Debug.Log("MenuMusicDestroyed");
     }
 }

Here is my second script.

 var targetScript : MenuMusicDontDestroy;
 
 function Awake () {
     targetScript.MenuMusicPlaying = false;
 }
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 ThePunisher · May 05, 2014 at 05:21 PM

This is easily solved by having an "initialize" scene that has a all your initialization scripts and things like your main camera that will persist throughout the entire game and usually have DontDestroyOnLoad set. Then a simple script in that scene can just transfer you to the main menu or whatever your first actual scene will be.

Comment
Add comment · Show 5 · 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 Bunny83 · May 05, 2014 at 05:52 PM 1
Share

Right, DontDestroyOnLoad should only be used in a scene that is loaded only once.

avatar image ThePunisher · May 05, 2014 at 05:57 PM 0
Share

Thanks for adding that, Bunny83. I forgot to mention it.

avatar image L1vD3v · May 05, 2014 at 06:45 PM 1
Share

Thanks for your response it lead me on the right way to fix it

avatar image ThePunisher · May 05, 2014 at 07:11 PM 0
Share

Glad to help.

avatar image fafase · May 05, 2014 at 07:22 PM 0
Share

I use DontDestroyOnLoad on scenes that can be loaded and reloaded, I just add some checks and logic to make it happen flawlessly.

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

22 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

Related Questions

Prevent countdown from reloading every time a new scene is loaded 1 Answer

Time.timeScale = 0 did not prevent click 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Java Script Dont Destroy On Load Duplicates Game Object 3 Answers

Can someone help me fix my Javascript for Flickering Light? 6 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