0

모두입니다. 저는 C# 및 단일화의 초보자입니다. 사전을 사용하여 오디오 관리자를 만들고 싶습니다. 그러나 "NullReferenceException"오류가 발생합니다. 블로우 코드.Dictionary <string, AudioSource> .Add()가 직접 초기화 되었기 때문에 오류가 발생합니다.

public Dictionary<string, AudioSource> AudioDictionary = new Dictionary<string, AudioSource>() ; 
private List<AudioSource> resAudioSource = new List<AudioSource>(); 
private const string ResourcePath = "Audio/"; 

private void Awake() 
{ 
    #region instance 
    if (instance == null) 
    { 
     instance = this; 
     DontDestroyOnLoad(gameObject); 
    } 
    else 
    { 
     Destroy(gameObject); 
    } 
    #endregion 

    AudioClip[] resAudio = Resources.LoadAll<AudioClip>(ResourcePath); 
    AudioSource temp; 

    for (int audioNum = 0; audioNum < resAudio.Length; audioNum++) 
    { 
     temp = gameObject.AddComponent<AudioSource>(); 
     Debug.Log(resAudio[audioNum].name); 
     AudioDictionary.Add(resAudio[audioNum].name, temp); 
    } 
} 

그리고 이와 같이 변경하면 정상입니다.

public Dictionary<string, AudioSource> AudioDictionary; 
private List<AudioSource> resAudioSource = new List<AudioSource>(); 
private const string ResourcePath = "Audio/"; 

private void Awake() 
{ 
    #region instance 
    if (instance == null) 
    { 
     instance = this; 
     DontDestroyOnLoad(gameObject); 
    } 
    else 
    { 
     Destroy(gameObject); 
    } 
    #endregion 

    AudioDictionary = new Dictionary<string, AudioSource>();//the change 
    AudioClip[] resAudio = Resources.LoadAll<AudioClip>(ResourcePath); 
    AudioSource temp; 

    for (int audioNum = 0; audioNum < resAudio.Length; audioNum++) 
    { 
     temp = gameObject.AddComponent<AudioSource>(); 
     Debug.Log(resAudio[audioNum].name); 
     AudioDictionary.Add(resAudio[audioNum].name, temp); 
    } 
} 

저는 사전을 직접 초기화 할 수없는 이유에 대해 매우 의아해합니다. 누구든지 설명 할 수 있습니까?

+1

'AudioDictionary'는 public 필드이므로'Awake()'메소드가 호출 될 때까지 다른 코드가 null로 설정하거나 지울 수 있습니다. 무엇에 액세스하고 있는지, 사전에 무엇이하고 있는지 확인하십시오. – CodingYoshi

+1

[this (overused) thread] (https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it)를 복제본으로 사용하는 것은 아닙니다. 매우 도움이됩니다. OP는 이미 오류의 원인이되는 행을 알고 있습니다. 문제는 왜 OP가 외부가 아닌 '깨어남'의 사전을 초기화해야 하는가입니다. 첫 번째 코드 스 니펫이 작동해야하는 것처럼 보이지만 Unity 전문가가 무게를 재어 볼 수 있습니다. –

+1

@GrantWinney 나는 당신과 동의합니다. 그 포스트는 남용되었습니다. ZJN에게 Unity 버전은 무엇입니까? 그리고 어떤 코드 라인에서 첫 번째 코드로 오류가 발생합니까? – Programmer

답변

0

2017.1.1f1 편집기 테스트에서 위에 언급 한 동작을 재현 할 수 없습니다. Assets/Resources/test.mp3 파일에 mp3 파일을 삽입했습니다. 그럼 난 테스트 스크립트를 빈 게임 오브젝트 생성 : for 루프에 중단 점을 삽입 할 때

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class Test : MonoBehaviour { 

public Dictionary<string, AudioSource> AudioDictionary = new Dictionary<string, AudioSource>() ; 
private List<AudioSource> resAudioSource = new List<AudioSource>(); 
private const string ResourcePath = "Audio/"; 

public static Test instance; 

private void Awake() 
{ 
    #region instance 
    if (instance == null) 
    { 
     instance = this; 
     DontDestroyOnLoad(gameObject); 
    } 
    else 
    { 
     Destroy(gameObject); 
    } 
    #endregion 

    AudioClip[] resAudio = Resources.LoadAll<AudioClip>(ResourcePath); 
    AudioSource temp; 

    for (int audioNum = 0; audioNum < resAudio.Length; audioNum++) 
    { 
     temp = gameObject.AddComponent<AudioSource>(); 
     Debug.Log(resAudio[audioNum].name); 
     AudioDictionary.Add(resAudio[audioNum].name, temp); 
    } 
} 
} 

을, 나는 AudioDictionary가 null이 아니었고 test.mp3가 AudioDictionary에 추가 된 것을 볼 수 있었다.

+0

위의 내용과 마찬가지로 Odin-inspector 플러그인 때문에 오류가 발생할 수 있습니다. @ 요정은 그가 어쩌면 옳은지 말하고있다. – ZJN

관련 문제