If the title wasn't clear enough (I don't really know how to explain it), basically I have an Audio Manager that is DontDestroyOnLoad. I also have a pause menu, that takes me back to the main menu, and in the main menu, I can reload the scene again. The problem is, upon reloading the scene, multiple copies of AudioManager is created in DontDestroyOnLoad, and I don't know how to solve it. My AudioManager script to give you a reference (only the important parts):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioManager : MonoBehaviour
{
#region Instances
private static AudioManager instance;
public static AudioManager Instance
{
get
{
if (instance == null) //if instance doesn't exist, find an AudioManager
{
instance = FindObjectOfType<AudioManager>();
if (instance == null) //if instance still doesn't exit, then it will create a new one
{
instance = new GameObject("Spawned AudioManager", typeof(AudioManager)).GetComponent<AudioManager>();
}
}
return instance;
}
private set
{
instance = value;
}
}
#endregion
#region Fields
//the audio source can only play one looping sound at a time
private AudioSource musicSource;
private AudioSource musicSource2; //purpose of having 2 musicSources is that we can use them to achieve effects like CrossFade
private AudioSource sfxSource;
private bool firstMusicSourceIsPlaying;
#endregion
private void Awake()
{
//Make sure we don't destroy this instance
DontDestroyOnLoad(this.gameObject);
//Create audio sources, and make them as references
musicSource = this.gameObject.AddComponent<AudioSource>();
musicSource2 = this.gameObject.AddComponent<AudioSource>();
sfxSource = this.gameObject.AddComponent<AudioSource>();
//loop musicSource
musicSource.loop = true;
musicSource2.loop = true;
}
If you are reading this, thanks for taking your time and try to help me!