1
\$\begingroup\$

I have an audio system set up wherein I have loaded all my audio clips centrally and play them on demand by passing the requesting audioSource into the sound manager.

However, there is a complication wherein if I want to overlay multiple looping sounds, I need to have multiple audio sources on an object, which is fine , so I created two in my script instantiated them and played my clips on them and then the world went crazy.

For some reason, when I create two audio Sources in an object only the latest one is ever used, even if I explicitly keep objects separated, playing a clip on one or the other plays the clip on the last one that was created, furthermore, either this last one is not created in the right place or somehow messes with the rolloff rules because I can hear it all across my level, havign just one source works fine, but putting a second one on it causes shit to go batshit insane.

Does anyone know the reason / solution for this ?

Some pseudocode :

guardSoundsSource = (AudioSource)gameObject.AddComponent("AudioSource");
guardSoundsSource.name = "Guard_Sounds_source";
// Setup this source 

guardThrusterSource = (AudioSource)gameObject.AddComponent("AudioSource"); 
guardThrusterSource.name = "Guard_Thruster_Source";
// setup this source
   // play using custom Sound manager      
soundMan.soundMgr.playOnSource(guardSoundsSource,"Guard_Idle_loop"
,true,GameManager.Manager.PlayerType);
   // this method prints out the name of the source the sound was to be played on and it always shows "Guard_Thruster_Source" even on the "Guard_Idle_loop" even though I clearly told it to use "Guard_Sounds_source"
\$\endgroup\$
  • 2
    \$\begingroup\$ When you set the .name property I believe you are setting/changing the name of the GameObject these component's are attached to, not the components themselves, so the name printed will always be the last name set. When you add an AudioSource via code it's going to have the default settings, which I think is 3D sound with default rolloff, so after adding the components you should set their properties how you want them. I think your code is working correctly, just the output is misleading. \$\endgroup\$ – Calvin Oct 21 '13 at 17:54
2
\$\begingroup\$

I can't speak exactly to the specific issue you are having, but I can say I am fairly certain you can add multiple of the same type of component to one GameObject. The best way I have found to do this is to access them is by using the "GetComponents" method call. Something like this should do:

AudioSource[] allMyAudioSources = GetComponents<AudioSource>();

This will return an array of AudioSources, which you can then assign to different variables and access independently without freaking things out. Something like this in C#:

AudioSource thrusterSource;
AudioSource guardSource;

void Start() {
    AudioSource[] allMyAudioSources = GetComponents<AudioSource>();
    thrusterSource = allMyAudioSources[0];
    guardSource = allMyAudioSources[1];
}

// These should no longer bug each other out!
guardSource.Play();
thrusterSource.Play();

EDIT: I thought it would be worth noting if it's not obvious, I have found the Components will populate the array in sequence from top to bottom. So it will help to keep that in mind when assigning those sound files.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.