0

all.Ich bin ein Anfänger von C# und unity.Ich möchte einen Audio-Manager mit dem Wörterbuch zu machen.Aber es tritt ein Fehler "NullReferenceException" mit blies Code.Dictionary <string, AudioSource> .Add() auftreten Fehler, weil direkt initialisiert

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); 
    } 
} 

Und es ist OK nach Änderung wie folgt.

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); 
    } 
} 

Ich bin sehr verwirrt, warum ich das Wörterbuch nicht direkt initialisieren kann, kann jeder es erklären?

+1

'AudioDictionary' ist ein öffentliches Feld, daher ist es möglich, dass die Methode 'Awake()' aufgerufen wird, wenn ein anderer Code sie auf null setzt oder löscht. Sehen Sie, was darauf zugreift und was es für Ihr Wörterbuch tut. – CodingYoshi

+1

Mit [dieses (überbenutzte) Thread] (https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-fix-it) als ein Duplikat zu diesem isn ' t sehr hilfreich. Das OP weiß bereits, welche Zeile den Fehler verursacht. Die Frage ist, warum muss das OP das Wörterbuch _inside_ von'Awake' anstatt von außen initialisieren? Es sieht für mich so aus, als ob das erste Code-Snippet funktionieren sollte, aber vielleicht könnte ein Unity-Experte ... –

+1

@GrantWinney Ich stimme dir zu. Dieser Beitrag wird überstrapaziert. An ZJN, Was ist deine Unity-Version? Und mit welcher Codezeile erhalten Sie diesen Fehler mit dem ersten Code? – Programmer

Antwort

0

Ich konnte das oben genannte Verhalten mit 2017.1.1f1 Editor Test nicht reproduzieren. Ich habe mp3-Datei in Assets/Resources/test.mp3-Datei eingefügt. Dann habe ich eine leere Gameobject mit Test-Skript erstellt:

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); 
    } 
} 
} 

Wenn Breakpoint in das Einfügen for-Schleife, konnte ich sehen, dass AudioDictionary war nicht null und test.mp3 wurde in AudioDictionary hinzugefügt.

+0

Vielen Dank für Ihren trail.Like, was meine oben genannten, kann der Fehler wegen der Odin-Inspector-Plugin auftreten. @CodingYoshi was er gesagt hat, vielleicht richtig. – ZJN

Verwandte Themen