- Home /
Music is playing through scenes, how I stop it when I need to?
Hello Unity dudes,
So i have 1 scene (Main Menu) with my audio source and three more levels with a bunch of scenes. Every level has an Audio source with this script that plays music across scenes.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BGSoundScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
//Play Global
private static BGSoundScript instance = null;
public static BGSoundScript Instance
{
get { return instance; }
}
void Awake()
{
if (instance != null && instance != this)
{
Destroy(this.gameObject);
return;
}
else
{
instance = this;
}
DontDestroyOnLoad(this.gameObject);
}
//Play Gobal End
// Update is called once per frame
void Update () {
}
}
It works like charm. But for example when you finish level 1 and you return to menu, you have 2 audios playing....when you start level 2 more audio source and so on.
So I would like to ask how do I manage this? Any thoughts?
Answer by jimmycrazyskills · Dec 22, 2018 at 05:06 PM
Hi mate, I might not understand what you're asking, but if you have multiple AudioSources then you could call AudioSource.Stop()
on the correct audioSource at the end of the level before you load the menu scene.
Hope this helped :)
Answer by stylianos-theodorikakos · Dec 22, 2018 at 05:18 PM
I will try to make it more clear. I have 1 scene which is my main menu with a song. When you click play you can choose up to 3 levels. Every level has multiple scenes, with different song for each level. When you finish a level you return to the menu and possible you would go for another level. How do I stop my audio source on the last scene which actually is a scene with a video player attached and changes scene when the video ends.
Your answer
![](https://koobas.hobune.stream/wayback/20220612184428im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to control an AudioSource with an AudioClip? 2 Answers
Audio while player is moving 0 Answers
Only one audio active. Others do not activate 2 Answers
Glitched Music 0 Answers
Is there a way to create a random Audiosource loop? 2 Answers