Heyo,
I have a gameobject with an audio source attached that I want to keep playing across scenes, for which I use the custom script below. The code is very simple, I only need it for this one piece of music, but I noticed that when I apply the script to one of AC's sound objects it doesn't work anymore, presumably because AC cleans up the objects with the sound script attached to them during a scene change? It's not really a problem in this instance, the other way works fine for me, but maybe this functionality would be nice to integrate into AC's Play Sound action so it can be used during cutscenes and such.
The script source (DontDestroy.cs):
public class DontDestroy : MonoBehaviour {
public static DontDestroy Instance {get;set;}
public bool playOnAwake;
void Awake() {
if (Instance == null) {
Instance = this;
DontDestroyOnLoad(this);
if (playOnAwake)
Play();
} else {
Destroy(this);
}
}
public void Play() {
if (audio.clip == null)
audio.Play();
}
}
Comments