public class AudioManager : MonoBehaviour
{
public Sound[] sounds;
+ public AudioSource currentSource;
+ public static AudioManager instance;
void Awake()
{
- foreach(Sound s in sounds)
+ if (instance == null)
+ {
+ instance = this;
+ }
+ else
+ {
+ Destroy(gameObject);
+ return;
+ }
+
+ DontDestroyOnLoad(gameObject);
+
+ foreach (Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
- }
+ }
}
public void Play(string name)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
- s.source.Play();
- }
-
- public void Stop(string name)
- {
- Sound s = Array.Find(sounds, sound => sound.name == name);
- s.source.Stop();
+ currentSource = s.source;
+ currentSource.Play();
}
-}
+}
\ No newline at end of file