- Home /
AudioSource Array Index is out of range
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.
Answer by MrAwesome343 · Dec 06, 2016 at 08:27 PM
I figured it out on my own. The answer is:
music[0] = variable
//not
variable = music[0]
Answer by hdtnl · Dec 03, 2016 at 09:40 AM
I think you need to drag and drop both Audio Source on Inspector to your Script direction. Watch this : https://unity3d.com/learn/tutorials/projects/2d-roguelike-tutorial/audio-and-sound-manager?playlist=17150
Answer by JC_SummitTech · Dec 04, 2016 at 08:31 AM
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.
Your answer
Follow this Question
Related Questions
Play sound from an array, not random, and only once 3 Answers
Can't get sound clip from array to play properly 1 Answer
make audio array and play random 1 Answer
Play Audio on destroy 3 Answers
How to read in Audio Input 1 Answer