proper C# casting for GetComponents on Audio Source?

hey there - kinda tearing my hair out on the proper C# casting to return an array of Audio Sources. it may be that Unity 4 may have changed format a bit.

i set up my audio source variables in the usual way:

AudioSource source1;
AudioSource source2;
private AudioSource[] sources;
public GameObject playlistObj;	

void Start(){
sources = (AudioSource[])playlistObj.GetComponents(typeof (AudioSource));
source1 = sources[0];
source2 = sources[1];
}

but this method while giving no syntax errors, returns a failed cast and then a null result later on.

i’ve seen some examples of adding AudioSources to a GameObject, but i’m more interested in retrieving a list of Audio Sources from a given object.

help appreciated!

scott

Actually, that’s anything but usual.
You’re simply overdoing stuff, which is not needed at all.

All you need to do is the following:

public AudioSource[] sources = new AudioSource[1];

You can then drag and drop all audio sources through the inspector in a list.
You don’t need to worry about the “1” part, thanks to the fact it’s public you can adjust it via the inspector at any time.