I’m trying to use an array to play different audioSources on the same object, but it’s giving me this error. I’ve looked around and, by what I’ve seen, I’m doing nothing wrong. Clearly I am, however, since it isn’t working at all. I really could use the help, please.
Here’s the code:
public AudioSource[] music;// = new AudioSource[1];
public AudioSource suspense, objection;
void Start(){
music = GetComponents<AudioSource>();
suspense = music[0];
objection = music[1];
}
I commented out the “= new AudioSource[1];” to test and neither of those worked.
You are attempting to access the first and second audioSources.
at line 5, you get all the AudioSources in the object in an array. That said, you may have less than 2 (can’t tell without more informations) so accessing the second one would give this error if you don’t have at least 2.
You could add after line 5
Debug.Log(“I have " + music.length + " AudioSources”);
To display how many available audioSources you have.