NullReferenceException: Object reference not set to an instance of an object SoundManager.Play (Define+Sound type, System.String path, System.Single pitch) (at Assets/Scripts/Managers/SoundManager.cs:64) TestSound.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/TestSound.cs:29) 에러가 발생합니다.
DontDestoryOnLoad에는 @Sound가 없고, @Manager만 있습니다.
현재 사운드 매니저는
public class SoundManager
{
AudioSource[] _audioSources = new AudioSource[(int)Define.Sound.MaxCount];
//MP3 Player -> AudioSource
//MP3 음원 -> AudioClip
//관객(귀) -> AudioListner
public void Init()
{
GameObject root= GameObject.Find("@Sound");
if(root = null)
{
root = new GameObject { name = "@Sound" };
Object.DontDestroyOnLoad(root);
string[] soundNames = System.Enum.GetNames(typeof(Define.Sound));
for(int i = 0; i<soundNames.Length -1; i++)
{
GameObject go = new GameObject { name = soundNames[i] };
_audioSources[i] = go.AddComponent<AudioSource>();
go.transform.parent = root.transform;
}
_audioSources[(int)Define.Sound.Bgm].loop = true;
}
}
public void Play(Define.Sound type,string path, float pitch = 1.0f)
{
if (path.Contains("Sounds/") == false)
path = $"Sounds/{path}";
if (type == Define.Sound.Bgm)
{
AudioClip audioClip = Managers.Resource.Load<AudioClip>(path);
if(audioClip == null)
{
Debug.Log($"AudioClip Missing !: {path}");
return;
}
//ToDo
}
else
{
AudioClip audioClip = Managers.Resource.Load<AudioClip>(path);
if(audioClip == null)
{
Debug.Log($"AudioClip Missing ! : {path}");
return;
}
AudioSource audioSource = _audioSources[(int)Define.Sound.Effect];
audioSource.pitch = pitch;
audioSource.PlayOneShot(audioClip);
}
}
이고,
Managers 스크립트는
public class Managers : MonoBehaviour
{
static Managers s_Instance; // 유일성이 보장
static Managers Instance { get { Init(); return s_Instance; } }//유일한 매니저를 갖고온다.
InputManager _input = new InputManager();
ResourceManager _resource = new ResourceManager();
SceneManagerEx _scene = new SceneManagerEx();
SoundManager _sound = new SoundManager();
UIManager _ui = new UIManager();
public static InputManager Input { get { return Instance._input; } }
public static ResourceManager Resource { get { return Instance._resource; } }
public static SceneManagerEx Scene { get { return Instance._scene; } }
public static SoundManager Sound { get { return Instance._sound; } }
public static UIManager UI { get { return Instance._ui; } }
void Start()
{
Init();
}
void Update()
{
_input.OnUpdate();
}
static void Init()
{
if(s_Instance == null)
{
GameObject go = GameObject.Find("@Managers");
if(go== null)
{
go = new GameObject { name = "@Managers" };
go.AddComponent<Managers>();
}
DontDestroyOnLoad(go);
s_Instance = go.GetComponent<Managers>();
s_Instance._sound.Init();
}
//초기화
}
}
입니다. 몇번 뒤져봐도 뭐가 잘못된지 모르겠습니다... 도와주세요!
2022. 05. 07. 11:41
감사합니다! ==로 바꾸니까 잘 나오네요! 빠른 답변 감사합니다!