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;
public AudioSource suspense, objection;
music = GetComponents<AudioSource>();
suspense = music;
objection = music;
I commented out the “= new AudioSource;” to test and neither of those worked.
I think you need to drag and drop both Audio Source on Inspector to your Script direction.
Watch this : Architecture and Polish - Unity Learn
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.
I figured it out on my own. The answer is:
music = variable
variable = music