all.I'm a beginner of c# and unity.I want to make an audio manager with the dictionary.But it occurs an error"NullReferenceException" with blew code.
 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);
     }
 }
And it's OK after change like this.
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);
    }
}
I'm very puzzled why I can't initialized the dictionary directly, Anybody can explain it?
 
    