Search Unity

How do i play two sounds at same time?

Discussion in 'Audio & Video' started by Epic-Username, Aug 26, 2015.

  1. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I'm using this script to play audio in the update method:

    Code (CSharp):
    1. void Update(){
    2. if(counter < 5)
    3. counter++;
    4. if(counter >= 5){
    5. ASources[0].Play();
    6. counter = 0;
    7. }
    8. }
    I want the sound to play even if the previous sound is still playing, but instead it just stops the previous one to play the new one how do i fix this?
     
  2. DenisLemos

    DenisLemos

    Joined:
    May 1, 2015
    Posts:
    787
  3. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Yes, but there's a downside to PlayOneShot. After you start a sound playing that way, you lose control over stopping it, changing the volume / pitch etc. You can only stop / change volume of ALL the one-shots at once. So for long sound effects, I find it unusable. Sometimes it's preferable to use multiple game objects all with their own Audio Source & Clip.
     
  4. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    Whateverthesay likes this.
  5. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    It's just a quick gun shot nothing long, so this wouldn't be a problem for me, but thanks for the tip.
     
  6. shumila

    shumila

    Joined:
    May 8, 2018
    Posts:
    1
  7. Whateverthesay

    Whateverthesay

    Joined:
    Apr 16, 2021
    Posts:
    1
  8. junk_rat_angel

    junk_rat_angel

    Joined:
    Dec 2, 2017
    Posts:
    3
    oneShot is doing exactly that for me. No matter what I do it stops the audio already playing even though they have different audio sources! somebody please help!
     
  9. SlattBurger01

    SlattBurger01

    Joined:
    Aug 6, 2021
    Posts:
    2
    I am not really sure If I understand your problem, but you should try AddComponent for each clip like this:
    Code (CSharp):
    1.     public void PlaySound(string soundName, float volume) // VOLUME IS 0 - 1
    2.     {
    3.         for (int i = 0; i < clips.Length; i++)
    4.         {
    5.             if (clips[i].name.ToUpper() == soundName.ToUpper())
    6.             {
    7.                 AudioSource source_ = gameObject.AddComponent<AudioSource>();
    8.                 Destroy(source_, 5);
    9.                 source_.clip = clips[i];
    10.                 source_.volume = volume;
    11.                 source_.Play();
    12.                 InstantiateSoundSphere(volume);
    13.             }
    14.         }
    15.     }