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 aman_jha · Sep 02, 2014 at 03:07 PM · c#androidaudioscenemusic

Keep audio playing even though I reset the scene

Hey Unitarians!

I have a simple game, and I have it that when you die, you have two options - to restart, or to go to the main menu. The main menu works fine and so does the restart. The way I do the restart is with a simple Application.LoadLevel (Application.loadedLevelName);. The problem is that the music gets reset.

Is there anyway to keep the music playing once I reset as if the scene wasn't reloaded?

Thanks, Aman Jha

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

5 Replies

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

Answer by kacyesp · Sep 02, 2014 at 03:13 PM

I think this is what you're looking for: http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

Just make sure in your script you don't reinstantiate the audio and make sure you only play the audio if it's not already playing, otherwise you'll restart it even if you don't destroy the object from the previous loaded scene.

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 aman_jha · Sep 02, 2014 at 03:21 PM 0
Share

This works great, but the problem is that the old gameobject that plays the audio stays, but the new one also gets loaded, which makes two audio players. How do I keep the old one and make sure the new one doesn't load?

avatar image AyAMrau · Sep 02, 2014 at 03:28 PM 1
Share

make a static field to keep reference to the object. when the object is spawned (so in Awake or Start) check if the static field is null - if it is assign the object, if it's not destroy the object that just spawned.

This way you are only keeping the first spawned object and destroying the copies.

avatar image kacyesp · Sep 02, 2014 at 03:30 PM 0
Share

That's also known as the singleton pattern, just in case you care :)

 if ( YourStaticGameObject == null )
     //create new game object
 else 
     //don't create new game object

I agree with what Aya said, except you're not "destroying" copies. They just never get created in the first place.

avatar image AyAMrau · Sep 02, 2014 at 03:45 PM 0
Share

Yes, that obviously removes the overhead of objects being created just to be destroyed :)

avatar image aman_jha · Sep 02, 2014 at 05:24 PM 0
Share

Works perfectly!! I didn't understand what you meant my static field but I just did a Gameobject.findwithtag on Awake xD

avatar image
3

Answer by W4rf4c3 · Jul 19, 2016 at 03:31 PM

  1. Create a GameObject with only the audioSource.

  2. Create a tag on that GameObject like in my example "gameMusic".

  3. Attach this script on it.

         using UnityEngine;
         using System.Collections;
         
         public class dontDestroyOnLoad : MonoBehaviour {
         
             private GameObject[] music;
         
             void Start(){
                 music = GameObject.FindGameObjectsWithTag ("gameMusic");
                 Destroy (music[1]);
             }
             
             // Update is called once per frame
             void Awake () {
                 DontDestroyOnLoad (transform.gameObject);
             }
         }
     
    
    
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
avatar image
2

Answer by Lesovoy · Feb 08, 2017 at 09:25 AM

 public class MusicPlayer : MonoBehaviour {
 
     static MusicPlayer instance = null;
 
     void Awake()
     {
         if (instance != null)
         {
             Destroy(gameObject);
         }
         else
         {
             instance = this;
             GameObject.DontDestroyOnLoad(gameObject);
         }
     }
 }

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
avatar image
1

Answer by AyAMrau · Sep 02, 2014 at 03:21 PM

You can use DontDestroyOnLoad on the object holding the audio source (make sure there aren't other things on the object that will cause issues if carried over).

Also keep in mind that if that object is placed in the scene manually, then you will need to destroy the newly spawned one on reload (otherwise every time you restart you will get an additional copy playing).

Comment
Add comment · Show 3 · 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 aman_jha · Sep 02, 2014 at 03:22 PM 0
Share

How do I destroy the new one without destroying the old??

avatar image Blcktfn · Sep 08, 2014 at 12:54 PM 2
Share

Hallo there,

I'll show you my result.

This is my BackgroudSoundScript. Create an empty gameobject name it "Game$$anonymous$$usic" with a tag "Game$$anonymous$$usic" Add the Script to the "Game$$anonymous$$usic" gameobject. And add the "Game$$anonymous$$usic" gameobject an AudioSource component.

It is not the best away but that work fine.

 using System.Collections;
 
 public class BGSound : $$anonymous$$onoBehaviour {
 
     GameObject[] musicObject;
 
     // Use this for initialization
     void Start () {
         musicObject = GameObject.FindGameObjectsWithTag ("Game$$anonymous$$usic");
         if (musicObject.Length == 1 ) {
                         audio.Play ();
         } else {
             for(int i = 1; i < musicObject.Length; i++){
                 Destroy(musicObject[i]);
             }
 
         }
             
 
     }
     
     // Update is called once per frame
     void Awake(){
         DontDestroyOnLoad (this.gameObject);
     }
 
 }
 
avatar image rileydabozo · Oct 21, 2014 at 12:39 AM 0
Share

Thanks Blcktfn that solution worked perfectly!

avatar image
0

Answer by sunwangshu · Apr 07, 2017 at 06:27 AM

Thanks @Lesovoy and @W4rf4c3, below is what worked for me (Inside GameController.cs of a GameController object in the reloaded scene):

 public AudioSource bgm; // link this with a bgm prefab, add tag "BGM", loop, don't play on awake
 
 void Awake() {
     GameObject currentBGM = GameObject.FindGameObjectWithTag ("BGM");
     if (currentBGM == null) {
         AudioSource spawned = Instantiate (bgm);
         spawned.Play ();
         DontDestroyOnLoad(spawned);
     }
 }

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 Colin_Peters · Oct 05, 2020 at 08:20 PM 0
Share

I know this thread is old, but I just wanted to let you know this worked for me. However, I'm having a problem accessing the instantiated game object. I keep getting an error message saying that the game object is in active. I'm trying to run a coroutine to get the audiosource to fadeout when leaving the menu scene and starting the game level. Do you know how to fix this? Thanks. I've been working on this for a couple days, but no luck.

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

29 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

Related Questions

Multiple Cars not working 1 Answer

Android play mp3 file 0 Answers

Continuous music on selected scenes? 2 Answers

Play Audio Through Scenes 0 Answers

Audio Scripting 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