Foreach loop only plays SFX once

  • Hi Everyone,

    The issue I am having is with playing sound for more than one cannon on a ship with a foreach loop. Everything else in the foreach loop works as expected. The issue is the sound only plays once.

    I spent a while searching for an answer before posting here. So far this is the only problem I have yet to find a solution to.

    I do have a video posted with more info.

    I you need anymore info let me know!!

    Thank you for your help :D


    YouTube Video Explaining Issue

    in case you need it...


  • Just a guess as I am not too experienced with unity yet but could it be that the sound starts over at every iteration so it actually plays multiple times but only a short time from the start before restarting? Maeby try increasing the interval of fire to test or set an audio source to all cannons

  • Thank you for your reply JJokelainen.

    If you are talking about the fire interval in the foreach loop; what that does is creates a small delay between the cannons so they don't all fire at the same time. I tried setting an audio source to all cannons and the audio still only played once.

    The solution was really simple and came to me as I was trying to fall asleep last night.

    I declared an AudioClip for the sound instead of an AudioSource.

    in the foreach loop I used audiosource.PlayOneShot(_shootCannon);

    This worked as expected.