- Home /
Question by
Noxury · May 22, 2015 at 11:00 PM ·
javascriptarrayaudio
Accessing Multiple AudioSources on GameObject JS
Hello,
I have 2 AudioSources on my GameObjects and trying to set their clip automatically by their type but with no success yet:
private var aud : AudioSource[] = new AudioSource[2];
private var a_horn : AudioSource;
private var a_engine : AudioSource;
function Start (){
aud = GetComponents(AudioSource);
a_horn = aud[0];
a_engine = aud[1];
if(transform.FindChild("Police")){
a_horn.clip = Resources.Load("Sounds/PoliceHorn") as AudioClip;
} else if(transform.FindChild("Boat")){
a_horn.clip = Resources.Load("Sounds/BoatHonk") as AudioClip;
}
}
Everytime it gives me an "InvalidCastException: Cannot cast from source type to destination type." Error
Comment
Answer by Eric5h5 · May 23, 2015 at 12:28 AM
GetComponents(Type) just returns Component[]. The generic version returns an array of the type.
GetComponents.<AudioSource>()