Search Unity

Multiple audio sources on a single gameObject

Discussion in 'Scripting' started by vidrohi, Oct 21, 2013.

  1. vidrohi

    vidrohi

    Joined:
    Dec 3, 2012
    Posts:
    14
    So,
    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 S*** to go batshit insane.

    Does anyone know the reason / solution for this ?

    Some pseudocode :
    Code (csharp):
    1.  
    2.     guardSoundsSource = (AudioSource)gameObject.AddComponent("AudioSource");
    3.     guardSoundsSource.name = "Guard_Sounds_source";
    4.     // Setup this source
    5.  
    6.     guardThrusterSource = (AudioSource)gameObject.AddComponent("AudioSource");
    7.     guardThrusterSource.name = "Guard_Thruster_Source";
    8.     // setup this source
    9.  
    10.    // play using custom Sound manager
    11.    soundMan.soundMgr.playOnSource(guardSoundsSource,"Guard_Idle_loop",true,GameManager.Manager.PlayerType);
    12.    // 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"
    13.  
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,175
    You're changing the name of the same GameObject twice. You can't name components, only GameObjects.

    --Eric
     
  3. vidrohi

    vidrohi

    Joined:
    Dec 3, 2012
    Posts:
    14
    So you're suggesting that the .name property is the parent game objects property and has nothing to do with the actual components ?
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,618
    thats what hes suggesting ;_)
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,175
    Oh, I am more than suggesting it, I am stating it as one of the grand truths of the universe. After all, would the holy writ of Unity documentation lie to us? Of course not, don't blaspheme.

    --Eric
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,618
    heh, no, however it does like to be vague at times.