Start of sound missing but end of sound plays (Solved)

I’ve got a problem with my sound when it first plays. When the following code plays, the very first part of the sound is just a crackle, and I only hear the end of the sound. The same thing happens if I remove the “audio.Stop” line completely.

private var sound = 0;
private var soundPlayed : String; 
var soundOne : AudioClip;
function Update (){
   if (sound == 0  soundPlayed != "soundOne") {
   audio.Stop();
   audio.PlayOneShot (soundOne);
      soundPlayed = "soundOne";
   }
   }

Bizarrely, if I alter the “audio.Stop” line like this, it plays the whole sound beginning to end, but this is obviously coded incorrectly and shows the coding error “Expressions in statements must only be executed for their side-effects”.

private var sound = 0;
private var soundPlayed : String; 
var soundOne : AudioClip;
function Update (){
   if (sound == 0  soundPlayed != "soundOne") {
   audio.Stop;
   audio.PlayOneShot (soundOne);
      soundPlayed = "soundOne";
   }
   }

Please can anyone advise me how can I re-code this so that the whole of the sound file plays from beginning to end without missing out the very start of the sound file?

If you’re trying to run it from the instant the game begins, you’ll run into the crackle effect. For some reason, any sound told to run from the beginning of a project’s startup will tend to do that.

To avoid it, put in some code for waiting a frame or two before you play the sound.

Gargerath,

Yes. I have that problem at the start of every level, then it’s OK.

I’ll do what you suggest and have a couple of seconds pause first.

Thanks a lot. Much appreciated.