- Home /
How to stop audio in a scene when DontDestroyOnLoad was already called?
Hello, so in my main menu scene and in my settings scene their is an object which holds the same audiosource (with the same audioclip), and this script called MyUnitySingleton:
private static MyUnitySingleton instance = null;
public static MyUnitySingleton 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
I also have my play scene with a different audiosource (and audioclip) in an object with no script. But I dont know how to not have two songs playing at the same time as I am already calling the DontDestroyOnLoad method. Any response is highly appreciated. Thank you in advance!
Answer by LEDWORKS · Jul 09, 2018 at 05:59 PM
If you want to stop when you load a new scene just call Stop() in the Start function
If you want to prevent tracks doubling, check IsPlaying() on the audiosource and only play when it is false https://docs.unity3d.com/ScriptReference/AudioSource-isPlaying.html