- Home /
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
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.
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?
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.
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.
Yes, that obviously removes the overhead of objects being created just to be destroyed :)
Works perfectly!! I didn't understand what you meant my static field but I just did a Gameobject.findwithtag on Awake xD
Answer by W4rf4c3 · Jul 19, 2016 at 03:31 PM
Create a GameObject with only the audioSource.
Create a tag on that GameObject like in my example "gameMusic".
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); } }
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);
}
}
}
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).
How do I destroy the new one without destroying the old??
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);
}
}
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);
}
}
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
Follow this Question
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