Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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,555
    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 :)