• 카테고리

    질문 & 답변
  • 세부 분야

    게임 프로그래밍

  • 해결 여부

    미해결

시작시 @Sound가 생성안되고있습니다.

22.05.05 23:56 작성 조회수 224

0

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();
                        
        }
        //초기화
        
    }
}
입니다. 몇번 뒤져봐도 뭐가 잘못된지 모르겠습니다... 도와주세요!

답변 1

답변을 작성해보세요.

0

if(root = null)

이 부분이 수상하네요.

root == null이 되어야 합니다.

이황욱님의 프로필

이황욱

질문자

2022.05.07

감사합니다! ==로 바꾸니까 잘 나오네요! 빠른 답변 감사합니다!