Playing audio through multiple scenes
Hi I found this video on YouTube, about playing music through multiple scenes without the sound breaking:
Playing Seamless Music Across Scenes in Unity 5 With Non-Destroyable Object
Here is the code from the video:
using UnityEngine;
using System.Collections;
public class DontDestroy : MonoBehaviour {
void Awake ()
{
GameObject[] objs = GameObject.FindGameObjectsWithTag("music");
if (objs.Length > 1)
Destroy(this.gameObject);
DontDestroyOnLoad(this.gameObject);
}
}
You attach it to the GameObject that has an audio, and create a tag for it so you can call it in script.
I was just wondering is it possible to add lines to the same code so that once it enters a certain scene, this GameObject stops appearing in it... the audio doesn't play for that scene...
I've asked the person on YouTube channel, and was told:
'To stop in a particular scene, I would test for the scene number and then if there's a match call a Stop() on the music, otherwise Play().'
But since I'm a code dummy, I have a problem of even writing that down (i.e. have no clue how it goes)... So, If there's someone to whom this ain't no problem to write, it'd be awesome to update this code (for music stopping at a particular scene).
Answer by YoYoYoYo_111_PiPi · Oct 08, 2016 at 07:04 PM
Okay, so this is the code I used, and it does what I wanted (Unity 5.4):
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class DontDestroy : MonoBehaviour {
void Awake ()
{
GameObject[] objs = GameObject.FindGameObjectsWithTag("music");
if (objs.Length > 1)
Destroy(this.gameObject);
DontDestroyOnLoad(this.gameObject);
}
void Update()
{
if (SceneManager.GetActiveScene().name == "SceneName")
{
Destroy(this.gameObject);
}
}
}
Instead of a 'SceneName', you write your own name of the scene where you want audio to stop. Hope this might help someone out. Peace.
EDIT: If you have multiple scenes to go from these, that keep playing the audio, and you want to stop it in multiple instances, just copy the...
if (SceneManager.GetActiveScene().name == "SceneName")
{
Destroy(this.gameObject);
}
... and add the other scene name.
YO, THAN$$anonymous$$ YOU SOO $$anonymous$$UCH FOR $$anonymous$$Y 2 DAY PROBLE$$anonymous$$S. IT WOR$$anonymous$$S VERY WELL. YOU'RE HERO. GOD BLESS YOU.
how about when back to the previous scene? Since the dontdestroyonload's object is destroyed, so the music won't play anymore, what if I want to play the music rat the previous scene? (Scene 1 > Scene 2 > Scene 3 ) - Destroy dontdestroyonload's object in scene 3, but want to play the same music when I switch from (Scene 3 > Scene 2/ Scene 1) without stop or restart the music.
Answer by skupasia · Oct 03, 2017 at 06:54 PM
@YoYoYoYo_111_PiPi I tried using this script, but I get this error:
Assets/Scripts/DontDestroy.cs(21,27): error CS0117: SceneManager' does not contain a definition for
GetActiveScene'
Do you know why it might be?
I am not sure, I haven't been using Unity lately. So i also haven't updated it either... I know that with new updates Unity also changes the code language. Though 'Scene$$anonymous$$anager.GetActiveScene' is still there in the official scripting documents:
Check if you have used everything correctly as in the code above (stuff like 'using UnityEngine.Scene$$anonymous$$anagement;') and you have to type the name of your scene, whatever it is called, ins$$anonymous$$d of "SceneName", as given in example. If you still have problems with it, please also check out the Unity forums for that error, because I can't say for myself that I'm a coding expert in any way, and I've used that one quite a while ago.
Cheers.
Do you have " using UnityEngine.Scene$$anonymous$$anagement; " at the top of your script?
Your answer
Follow this Question
Related Questions
Adjusting Audio Volume via UI Slider 0 Answers
Additive Scenes, best way to hide content? 1 Answer
Cross-Scene Simulation? 0 Answers