Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
8
Question by Zitoox · Oct 21, 2016 at 08:25 PM · soundplayscenesthroughother

Make music continue playing through scenes

Hi! I have an audio source attached to an object in my main menu scene, and i wanted it's music to keep playing even when you are in SOME other scenes. For an example:

The main song is playing. The player is in the Main Menu scene and the music is at 1:43. The player clicks on Select Map and goes to the Map Selection scene. Then, the music continues from 1:43 until it finishes. As it has the loop, it plays again and again.

I don't want the song to play in all the scenes, just some specific. Do someone know how to do this? I don't have any idea of how to make this...

Comment
Add comment · Show 2
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 MarkTailor · Jan 25, 2021 at 03:51 PM 0
Share

I'm also interested in it. I would like to get some more details about it. I hope someone will help.

avatar image datnasty2_0 · Jul 12, 2021 at 08:33 PM 0
Share

I would like to know how to fix this also.

8 Replies

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

Answer by Denvery · Oct 21, 2016 at 09:06 PM

Let's try this.

Create GameObject and assign any custom tag. For example, create tag "Music" and assign. Attach script to this game object and make DontDestroyOnLoad:

 using UnityEngine;
 
 public class MusicClass : MonoBehaviour
 {
     private AudioSource _audioSource;
     private void Awake()
     {
         DontDestroyOnLoad(transform.gameObject);
         _audioSource = GetComponent<AudioSource>();
     }
 
     public void PlayMusic()
     {
         if (_audioSource.isPlaying) return;
         _audioSource.Play();
     }
 
     public void StopMusic()
     {
         _audioSource.Stop();
     }
 }


Attach your audio source to this object.

From scenes, where you want to play music, call method:

 GameObject.FindGameObjectWithTag("Music").GetComponent<MusicClass>().PlayMusic();

From scenes, where you don't want to play music, call method:

 GameObject.FindGameObjectWithTag("Music").GetComponent<MusicClass>().StopMusic();
Comment
Add comment · Show 18 · 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 LawnDowe · Mar 03, 2020 at 07:53 AM 0
Share

This worked for me, but when I went back to one of the scenes, it played the music again over the first time it played, and this happened whenever I went into that menu. And I doing something wrong?

avatar image john601_unity · Dec 14, 2020 at 07:12 PM 0
Share

I have no idea how to call the method. Im still very new too unity and coding as a whole and I am very confused.

avatar image darkwindgame2020 · Dec 24, 2020 at 04:56 PM 0
Share

Thank you So $$anonymous$$uch its working very well.

avatar image kimn_unity · Jan 18, 2021 at 03:06 PM 0
Share

this doesn't work with the audio source with the loop true though

avatar image NuffuruDevelopment · Feb 22, 2021 at 01:49 PM 5
Share

I would change it to this, because this way if you go back to the scene where the music object was placed it won't make another music object.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Music : MonoBehaviour
 {
     private AudioSource _audioSource;
     private GameObject[] other;
     private bool NotFirst = false;
     private void Awake()
     {
         other = GameObject.FindGameObjectsWithTag("Music");
 
         foreach (GameObject oneOther in other)
         {
             if (oneOther.scene.buildIndex == -1)
             {
                 NotFirst = true;
             }
         }
 
         if (NotFirst == true)
         {
             Destroy(gameObject);
         }
         DontDestroyOnLoad(transform.gameObject);
         _audioSource = GetComponent<AudioSource>();
     }
 
     public void PlayMusic()
     {
         if (_audioSource.isPlaying) return;
         _audioSource.Play();
     }
 
     public void StopMusic()
     {
         _audioSource.Stop();
     }
 }
 
avatar image RonaltrixJGachet NuffuruDevelopment · Apr 24, 2021 at 12:21 AM 0
Share

Hello, I have tried using your codes but every time I go back to the scene where I created the base game object, the game object multiplies.

Thanks you

Show more comments
avatar image
3

Answer by elvirais · Oct 23, 2016 at 09:18 PM

This works for me, latest version of unity: (C# code) (I also found this code somewhere on this forum :)

 using UnityEngine;
 using System.Collections;
 
 public class GameMusicPlayer : MonoBehaviour
 {
 
     private static GameMusicPlayer instance = null;
     public static GameMusicPlayer Instance
     {
         get { return instance; }
     }
     void Awake()
     {
         if (instance != null && instance != this) {
             Destroy(this.gameObject);
             return;
         } else {
             instance = this;
         }
         DontDestroyOnLoad(this.gameObject);
     }
     // any other methods you need


}

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 VinnyInTheLab · Sep 10, 2018 at 03:16 AM 0
Share

This code works for me as well, however, how to I stop the music from playing when entering a scene where I don't want the music to play?

avatar image elvirais · Jan 18, 2021 at 04:29 PM 0
Share

To stop the music in the new scene, you'll need a link to the gameobject. Since it comes over from another scene, I used a GameObject.Find or .FindWithTag function to get the object:

https://docs.unity3d.com/ScriptReference/GameObject.html

avatar image
3

Answer by keszeial · Jan 24, 2021 at 03:42 PM

First thread I found when looking for a solution to this problem, so I'll add to this thread.

Here is a working solution that avoids the use of singletons, allowing you to add the script to multiple objects in the scene and tweak the behaviour via the inspector:

 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 /// <summary>
 /// Attach this component to objects that you want to keep alive (e.g. theme songs) in certain scene transitions. 
 /// For reusability, this component uses the scene names as strings to decide whether it survives or not after a scene is loaded 
 /// </summary>
 public class DontDestroyOnSelectedScenes : MonoBehaviour
 {
     [Tooltip("Names of the scenes this object should stay alive in when transitioning into")]
     public List<string> sceneNames;
 
     [Tooltip("A unique string identifier for this object, must be shared across scenes to work correctly")]
     public string instanceName;
 
     // for singleton-like behaviour: we need the first object created to check for other objects and delete them in the scene during a transition
     // since Awake() callback preceded OnSceneLoaded(), place initialization code in Start()
     private void Start()
     {
         DontDestroyOnLoad(this.gameObject);
 
         // subscribe to the scene load callback
         SceneManager.sceneLoaded += OnSceneLoaded;
     }
 
     void OnSceneLoaded(Scene scene, LoadSceneMode mode)
     {
         // delete any potential duplicates that might be in the scene already, keeping only this one 
         CheckForDuplicateInstances();
 
         // check if this object should be deleted based on the input scene names given 
         CheckIfSceneInList();
     }
 
     void CheckForDuplicateInstances()
     {
         // cache all objects containing this component
         DontDestroyOnSelectedScenes[] collection = FindObjectsOfType<DontDestroyOnSelectedScenes>();
 
         // iterate through the objects with this component, deleting those with matching identifiers
         foreach (DontDestroyOnSelectedScenes obj in collection)
         {
             if(obj != this) // avoid deleting the object running this check
             {
                 if (obj.instanceName == instanceName)
                 {
                     Debug.Log("Duplicate object in loaded scene, deleting now...");
                     DestroyImmediate(obj.gameObject);
                 }
             }
         }
     }
 
     void CheckIfSceneInList()
     {
         // check what scene we are in and compare it to the list of strings 
         string currentScene = SceneManager.GetActiveScene().name;
 
         if (sceneNames.Contains(currentScene))
         {
             // keep the object alive 
         }
         else
         {
             // unsubscribe to the scene load callback
             SceneManager.sceneLoaded -= OnSceneLoaded;

             DestroyImmediate(this.gameObject);
         }
     }
 }
 

Just add this script as if it were a component to the parent object (e.g. in the image, the empty game object carrying all the audio sources: "Audio") and write down the name of the scenes it should survive in, then give it a unique string identifier relative to all other scripts in the scene using this component. alt text


dont-destroy-on-load.png (64.4 kB)
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 SpyderGamer · Oct 26, 2021 at 03:03 PM 0
Share

This actually worked! I've been looking for the right script for a long time! Thank you man :)

avatar image
0

Answer by hexagonius · Oct 21, 2016 at 09:06 PM

Have a look at this post:

http://answers.unity3d.com/questions/11314/audio-or-music-to-continue-playing-between-scene-c.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 Zitoox · Oct 22, 2016 at 03:19 PM -1
Share

These two answers have outdated scripts.

avatar image
0

Answer by nathanthesnooper · Oct 23, 2016 at 11:15 PM

This should work. Remember you can always save the Time Elapsed in a PlayerPrefs alternatively.

 void Start () {
     DontDestroyOnLoad(this.gameObject);
 }

Not tested yet but should work.

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 Hobbitgy8 · Jan 17, 2020 at 02:56 PM 0
Share

this doesn't seem to work for me

  • 1
  • 2
  • ›

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

80 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

Related Questions

make music continue play across scenes except one scenes 1 Answer

Audio Playing upon death 1 Answer

How do I stop gameobjects from going through each other in the scene unity3d 1 Answer

gameObject to play sound on deletion, won't work... 1 Answer

Can't get my sound to work.. 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