- Home /
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
You should use the generic overload of GetComponents
is there a place that that overload is posted, or is that something i'd find in $$anonymous$$SDN or DotNetPerls?
The Unity docs, like everything else in the Unity API
Answer by Blaveloper · Jan 19, 2014 at 12:08 PM
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.
This doesn't answer the question and it is definitely usual to use GetComponent
! There is no reason to do something manually when you can do it via code
I wasn't trying to be rude (and neither do I think I was), I was simply pointing out why your answer is not appropriate for this question.
there's one issue with the answer that you're posting. this is for an object that contains more than one AudioSource component on it. so i need to know the order of AudioSources and assign them accordingly. hence the casting for a list of Components. if every AudioSource i was looking for was on a separate GameObject, i could use your method.
so, i'm just really curious in C# if you have multiple copies of one type of Component on a specific GameObject, how do do you return the list of those Components so you can order them and then address them specifically?
Do you mean it as a comment to me? If so, I simply make multiple objects with one audio source each. Then I drag each object to the array list I specified before through the inspector.
i understand your method and i personally agree with the basic idea (one game object for each Source). however i'm using another developer's tool. they have an object that has two AudioSources on it, in order to crossfade audio between two music clips. so i need to be able to address either Source independently.
This is not an answer to the question. Please post comments as comments and not as answers.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
AudioClip stuttering while loop is OFF and not using function Update? 1 Answer
Change AudioSource output via Script 1 Answer
Audio Manager or not? Audio System 0 Answers