NullReferenceException: Object reference not set to an instance of an object ... Why?
This is my Code:
public void addAudio(AudioSource a) { for (int i = 0; i < audioSources.Length; i++) { if (audioSources[i] == null) { Debug.Log(a); audioSources[i] = a; volumes[i] = a.volume; break; } } refreshSoundLevel(); } public void refreshSoundLevel() { for (int i = 0; i < audioSources.Length; i++) { if (audioSources[i] != null) { audioSources[i].volume = volumes[i] * (Options.getSoundLev() / 100); } } }
The error is called on volumes[i] = a.volume;
. The AudioSource a is not null and the game still works.
I don't know how to fix this. Can you help me?
Answer by tanoshimi · Jul 23, 2017 at 09:25 AM
Because volumes
is null.
@tanoshimi I have defined it before as float[] volumes = new float[audioSources.Length];
, so it shouldn't be null.
Your answer
![](https://koobas.hobune.stream/wayback/20220612130922im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Null Reference Exception Error 1 Answer
There's an issue with this script I can't find 0 Answers
wtf does this error mean 1 Answer
Array index is out of range (C#)? 1 Answer
NullReferenceException, Script Error 1 Answer