Search Unity

Keep Sound Player After New Sound is Played

Discussion in 'Audio & Video' started by cjburkey01, Dec 31, 2014.

  1. cjburkey01

    cjburkey01

    Joined:
    Nov 8, 2013
    Posts:
    26
    I'm making a gun script, and for an automatic gun the sound gets cut off when the sound is played again.
    So, if I call
    Code (csharp):
    1. source.Play();
    , how do I keep the sound, playing before, playing. So it doesn't chop the sound off and play the new one?
     
  2. MakeCodeNow

    MakeCodeNow

    Joined:
    Feb 14, 2014
    Posts:
    1,246
    Try AudioSource.PlayOneShot(). That creates a temporary GameObject/AudioSource w/ the specified clip and position, which you could also do yourself if you wanted more control over the sounds (though you probably don't for a machine gun).
     
  3. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    PlayOneShot is cool for its "easiness", but it has drawbacks. It is totally "fire and forget", meaning if you have 5 one shots playing on the same Audio Source, you cannot stop any one of them. You only can stop all of them. So I think it's really only useful for very short sounds that would never need to be stopped.
     
  4. cjburkey01

    cjburkey01

    Joined:
    Nov 8, 2013
    Posts:
    26
    Thank you :)