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
2
Question by reecefox · Feb 07, 2015 at 03:59 PM · cubemusicdontdestroyonload

Don't Destroy on Load return to scene Duplicate Music Object

I have a main menu scene and a options menu scene. I have a cube with no mesh rendered and an audio source with a 7 minute sound track that loops (the cube is in the main menu scene(first scene on start-up)). I have a script for the cube music player object that is do not destroy object on load when you switch to options menu. It works, when you switch to options menu, but when you switch back to main menu from options it creates another duplicate music playing cube so there is the don't destroy on load music cube that has been playing since beginning, and the new cube that restarts the track and now there are two versions playing. I need the same track that starts at the beginning when the game starts to keep playing when you switch to options menu and back to main menu, so that same track is always playing without duplicating or restarting. Please help, this bug is making me very distressed, as I and probably every other game developer have OCD and can not move on without fixing this. Thank you, Reece.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
15

Answer by Lo0NuhtiK · Feb 07, 2015 at 04:28 PM

http://wiki.unity3d.com/index.php/Singleton

...and a more simple script for you to get started with ->

 public class MusicPlayer : MonoBehaviour
 {
     private static MusicPlayer _instance ;
 
     void Awake()
     {
         //if we don't have an [_instance] set yet
         if(!_instance)
             _instance = this ;
         //otherwise, if we do, kill this thing
         else
             Destroy(this.gameObject) ;
 
 
         DontDestroyOnLoad(this.gameObject) ;
     }
 }
Comment
Add comment · Show 2 · 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 burnout156 · Jul 10, 2018 at 12:54 PM 0
Share

thanks bro, you help very much

avatar image aletl10 · Feb 22, 2019 at 11:16 AM 0
Share

TY u help me a lot.

avatar image
0

Answer by DiegoSLTS · Feb 07, 2015 at 06:43 PM

Do not add the GameObject in the Main Menu scene using the editor, instantiate it with code on the Start or Awake method of a script that runs on Main Menu. Before the instatiation code, check that the GameObject for the music is not present so you only add it once: http://docs.unity3d.com/ScriptReference/GameObject.Find.html.

Next time you return to the scene the GameObject will already be there, so the code won't create a clone.

You can create a new GameObject and set the music component in code, or you can create a prefab of your current music GameObject and the instantiation takes just one line of code: http://docs.unity3d.com/Manual/InstantiatingPrefabs.html

Comment
Add comment · Show 1 · 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 tkamruzzaman · Oct 05, 2015 at 08:55 AM 0
Share

there is a dot in the link. edited link: http://docs.unity3d.com/ScriptReference/GameObject.Find.html

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Continuous AudioSource in DontDestroyOnLoad 1 Answer

Object.DontDestroyOnLoad ? 2 Answers

How to stop audio in a scene when DontDestroyOnLoad was already called? 1 Answer

How to STOP music when loading a SPECIFIC SCENE? 3 Answers

Don't destroy a variable when changing scene 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